Skip to content

Commit 1f6835e

Browse files
committed
change approach; now each view has graphics context assigned in View.init
1 parent 753ebe9 commit 1f6835e

File tree

91 files changed

+957
-833
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+957
-833
lines changed

editor/nimxedit.nim

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
import tables
2-
3-
import nimx/matrixes
4-
import nimx/system_logger
5-
import nimx/animation
6-
import nimx/image
7-
import nimx/window
8-
import nimx/autotest
9-
import nimx/button, nimx/text_field
10-
import nimx/all_views
11-
import nimx/editor/edit_view
12-
13-
const isMobile = defined(ios) or defined(android)
1+
import nimx
142

153
proc runAutoTestsIfNeeded() =
164
uiTest generalUITest:
@@ -22,7 +10,7 @@ proc runAutoTestsIfNeeded() =
2210
startRegisteredTests()
2311

2412
proc startApplication() =
25-
when isMobile:
13+
when mobile:
2614
var mainWindow = newFullscreenWindow()
2715
else:
2816
var mainWindow = newWindow(newRect(40, 40, 1200, 600))

kiwi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 64d1c62c1eb07538371990e15a257bcb5fb4b7a9

nim_emscripten_tutorial

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 0640d403bf84f6eac78da971006252463a808c20

nimsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 491120cefac5bcf1bd7fbaa096ff80cd7c688d92

nimx.nim

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import nimx / [
2+
backends,
3+
window,
4+
autotest,
5+
]
6+
7+
# Views
8+
import nimx / [
9+
button,
10+
text_field,
11+
image_view,
12+
slider,
13+
segmented_control,
14+
collection_view,
15+
editor/edit_view,
16+
]
17+
18+
export
19+
# core
20+
backends,
21+
window,
22+
autotest,
23+
24+
# views
25+
button,
26+
text_field,
27+
image_view,
28+
slider,
29+
segmented_control,
30+
collection_view,
31+
edit_view

nimx.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "0.1"
3+
version = "0.2"
44
author = "Yuriy Glukhov"
55
description = "GUI framework"
66
license = "MIT"

nimx/abstract_window.nim

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ method drawWindow*(w: Window) {.base.} =
8383
w.needsDisplay = false
8484

8585
w.recursiveDrawSubviews()
86-
let c = w.gfxCtx
86+
template c: untyped = w.gfx
8787

8888
let profiler = sharedProfiler()
8989
if profiler.enabled:
9090
updateFps()
91-
profiler["Overdraw"] = getOverdrawValue(w.gfxCtx)
92-
profiler["DIPs"] = getDIPValue(w.gfxCtx)
91+
profiler["Overdraw"] = getOverdrawValue(w.gfx)
92+
profiler["DIPs"] = getDIPValue(w.gfx)
9393
profiler["Animations"] = totalAnims
9494

9595
const fontSize = 14
@@ -125,7 +125,7 @@ method drawWindow*(w: Window) {.base.} =
125125
c.drawRect(rect)
126126

127127
method draw*(w: Window, rect: Rect) =
128-
let gl = w.gfxCtx.gl
128+
template gl: untyped = w.gfx.gl
129129
if w.mActiveBgColor != w.backgroundColor:
130130
gl.clearColor(w.backgroundColor.r, w.backgroundColor.g, w.backgroundColor.b, w.backgroundColor.a)
131131
w.mActiveBgColor = w.backgroundColor
@@ -202,12 +202,14 @@ proc onFocusChange*(w: Window, inFocus: bool)=
202202
sharedNotificationCenter().postNotification(AW_FOCUS_LEAVE)
203203

204204
var newWindow*: proc(r: Rect): Window
205+
var newWindowWithGfxContext*: proc(gfx: GraphicsContext, r: Rect): Window
205206
var newFullscreenWindow*: proc(): Window
207+
var newFullscreenWindowWithGfxContext*: proc(gfx: GraphicsContext): Window
206208
var newWindowWithNative*: proc(handle: pointer, r: Rect): Window
207209
var newFullscreenWindowWithNative*: proc(handle: pointer): Window
208210

209-
method init*(w: Window, _: Window, frame: Rect) =
210-
procCall w.View.init(w, frame)
211+
method init*(w: Window, gfx: GraphicsContext, frame: Rect) =
212+
procCall w.View.init(gfx, frame)
211213
w.window = w
212214
w.needsDisplay = true
213215
w.mCurrentTouches = newTable[int, View]()

nimx/autotest.nim

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import macros, logging, strutils
2-
import nimx / [ timer, app, event, abstract_window, button ]
2+
import nimx / [ backends, timer, app, event, window, button ]
33

44
type UITestSuiteStep* = tuple
55
code : proc()
@@ -10,8 +10,6 @@ type UITestSuite* = ref object
1010
name: string
1111
steps: seq[UITestSuiteStep]
1212

13-
const web = defined(js) or defined(emscripten) or defined(wasm)
14-
1513
when web:
1614
when defined(emscripten) or defined(wasm):
1715
import jsbind/emscripten

nimx/backends.nim

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
type OsApi* {.pure.} = enum
2+
web
3+
android
4+
ios
5+
macosx
6+
linux
7+
windows
8+
9+
type WindowApi* {.pure.} = enum
10+
web
11+
sdl
12+
x11
13+
appkit
14+
winapi
15+
16+
type InputApi* {.pure.} = enum
17+
web
18+
sdl
19+
x11
20+
appkit
21+
winapi
22+
23+
type GraphicApi* {.pure.} = enum
24+
opengles2
25+
26+
type AudioApi* {.pure.} = enum
27+
web
28+
sdl
29+
appkit
30+
winapi
31+
32+
type Backend* = tuple
33+
os: OsApi
34+
win: WindowApi
35+
input: InputApi
36+
gfx: GraphicApi
37+
audio: AudioApi
38+
39+
const backend*: Backend =
40+
when defined js:
41+
(OsApi.web, WindowApi.web, InputApi.web, GraphicApi.opengles2, AudioApi.web)
42+
elif defined emscripten:
43+
(OsApi.web, WindowApi.web, InputApi.web, GraphicApi.opengles2, AudioApi.web)
44+
elif defined wasm:
45+
(OsApi.web, WindowApi.web, InputApi.web, GraphicApi.opengles2, AudioApi.web)
46+
elif defined ios:
47+
(OsApi.ios, WindowApi.sdl, InputApi.sdl, GraphicApi.opengles2, AudioApi.sdl)
48+
elif defined android:
49+
(OsApi.android, WindowApi.sdl, InputApi.sdl, GraphicApi.opengles2, AudioApi.sdl)
50+
elif defined macosx:
51+
(OsApi.macosx, WindowApi.sdl, InputApi.sdl, GraphicApi.opengles2, AudioApi.sdl)
52+
elif defined linux:
53+
(OsApi.linux, WindowApi.sdl, InputApi.sdl, GraphicApi.opengles2, AudioApi.sdl)
54+
elif defined windows:
55+
(OsApi.windows, WindowApi.sdl, InputApi.sdl, GraphicApi.opengles2, AudioApi.sdl)
56+
else: {.error: "unknown backend".}
57+
58+
59+
const web* = backend.os == OsApi.web
60+
const mobile* = defined(ios) or defined(android)

nimx/button.nim

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,38 +52,38 @@ Button.properties:
5252
imageMarginLeft
5353
image
5454

55-
proc newButton*(w: Window, r: Rect): Button =
55+
proc newButton*(gfx: GraphicsContext, r: Rect): Button =
5656
result.new()
57-
result.init(w, r)
57+
result.init(gfx, r)
5858

59-
proc newButton*(parent: View = nil, w: Window, position: Point = newPoint(0, 0), size: Size = newSize(100, 20), title: string = "Button"): Button =
60-
result = newButton(w, newRect(position.x, position.y, size.width, size.height))
59+
proc newButton*(parent: View = nil, gfx: GraphicsContext, position: Point = newPoint(0, 0), size: Size = newSize(100, 20), title: string = "Button"): Button =
60+
result = newButton(gfx, newRect(position.x, position.y, size.width, size.height))
6161
result.title = title
6262
if not isNil(parent):
6363
parent.addSubview(result)
6464

65-
proc newCheckbox*(w: Window, r: Rect): Button =
66-
result = newButton(w, r)
65+
proc newCheckbox*(gfx: GraphicsContext, r: Rect): Button =
66+
result = newButton(gfx, r)
6767
result.style = bsCheckbox
6868
result.behavior = bbToggle
6969

70-
proc newRadiobox*(w: Window, r: Rect): Button =
71-
result = newButton(w, r)
70+
proc newRadiobox*(gfx: GraphicsContext, r: Rect): Button =
71+
result = newButton(gfx, r)
7272
result.style = bsRadiobox
7373
result.behavior = bbToggle
7474

75-
proc newImageButton*(w: Window, r: Rect): Button =
76-
result = newButton(w, r)
75+
proc newImageButton*(gfx: GraphicsContext, r: Rect): Button =
76+
result = newButton(gfx, r)
7777
result.style = bsImage
7878

79-
proc newImageButton*(parent: View = nil, w: Window, position: Point = newPoint(0, 0), size: Size = newSize(100, 20), image: Image = nil): Button =
80-
result = newImageButton(w, newRect(position.x, position.y, size.width, size.height))
79+
proc newImageButton*(parent: View = nil, gfx: GraphicsContext, position: Point = newPoint(0, 0), size: Size = newSize(100, 20), image: Image = nil): Button =
80+
result = newImageButton(gfx, newRect(position.x, position.y, size.width, size.height))
8181
result.image = image
8282
if not isNil(parent):
8383
parent.addSubview(result)
8484

85-
method init*(b: Button, w: Window, frame: Rect) =
86-
procCall b.Control.init(w, frame)
85+
method init*(b: Button, gfx: GraphicsContext, frame: Rect) =
86+
procCall b.Control.init(gfx, frame)
8787
b.state = bsUp
8888
b.enabled = true
8989
b.backgroundColor = whiteColor()
@@ -93,22 +93,22 @@ method init*(b: Button, w: Window, frame: Rect) =
9393
b.imageMarginTop = 2
9494
b.imageMarginBottom = 2
9595

96-
method init*(b: Checkbox, w: Window, frame: Rect) =
97-
procCall b.Button.init(w, frame)
96+
method init*(b: Checkbox, gfx: GraphicsContext, frame: Rect) =
97+
procCall b.Button.init(gfx, frame)
9898
b.style = bsCheckbox
9999
b.behavior = bbToggle
100100

101-
method init*(b: Radiobox, w: Window, frame: Rect) =
102-
procCall b.Button.init(w, frame)
101+
method init*(b: Radiobox, gfx: GraphicsContext, frame: Rect) =
102+
procCall b.Button.init(gfx, frame)
103103
b.style = bsRadiobox
104104
b.behavior = bbToggle
105105

106106
proc drawTitle(b: Button, xOffset: Coord) =
107-
template gfxCtx: untyped = b.window.gfxCtx
108-
template fontCtx: untyped = b.window.gfxCtx.fontCtx
109-
template gl: untyped = b.window.gfxCtx.gl
107+
template gfx: untyped = b.gfx
108+
template fontCtx: untyped = b.gfx.fontCtx
109+
template gl: untyped = b.gfx.gl
110110
if b.title.len != 0:
111-
gfxCtx.fillColor = if b.state == bsDown and b.style == bsRegular:
111+
gfx.fillColor = if b.state == bsDown and b.style == bsRegular:
112112
whiteColor()
113113
else:
114114
blackColor()
@@ -117,7 +117,7 @@ proc drawTitle(b: Button, xOffset: Coord) =
117117
var titleRect = b.bounds
118118
var pt = centerInRect(sizeOfString(fontCtx, gl, font, b.title), titleRect)
119119
if pt.x < xOffset: pt.x = xOffset
120-
gfxCtx.drawText(font, pt, b.title)
120+
gfx.drawText(font, pt, b.title)
121121

122122
var regularButtonComposition = newComposition """
123123
uniform vec4 uStrokeColor;
@@ -136,7 +136,7 @@ void compose() {
136136
"""
137137

138138
proc drawRegularBezel(b: Button) =
139-
draw b.window.gfxCtx, regularButtonComposition, b.bounds:
139+
draw b.gfx, regularButtonComposition, b.bounds:
140140
if b.state == bsUp:
141141
setUniform("uStrokeColor", newGrayColor(0.78))
142142
setUniform("uFillColorStart", if b.enabled: b.backgroundColor else: grayColor())
@@ -164,7 +164,7 @@ proc drawCheckboxStyle(b: Button, r: Rect) =
164164
let
165165
size = b.bounds.height
166166
bezelRect = newRect(0, 0, size, size)
167-
c = b.window.gfxCtx
167+
c = b.gfx
168168

169169
if b.value != 0:
170170
draw c, checkButtonComposition, bezelRect:
@@ -208,7 +208,7 @@ void compose() {
208208

209209
proc drawRadioboxStyle(b: Button, r: Rect) =
210210
let bezelRect = newRect(0, 0, b.bounds.height, b.bounds.height)
211-
let c = b.window.gfxCtx
211+
template c: untyped = b.gfx
212212

213213
# Selected
214214
if b.value != 0:
@@ -227,7 +227,7 @@ proc drawRadioboxStyle(b: Button, r: Rect) =
227227
b.drawTitle(bezelRect.width + 1)
228228

229229
proc drawImage(b: Button) =
230-
let c = b.window.gfxCtx
230+
template c: untyped = b.gfx
231231
let r = b.bounds
232232
if b.imageMarginLeft != 0 or b.imageMarginRight != 0 or
233233
b.imageMarginTop != 0 or b.imageMarginBottom != 0:

0 commit comments

Comments
 (0)