Skip to content
This repository has been archived by the owner on Jan 27, 2022. It is now read-only.

Commit

Permalink
small optimize ❤️
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Nov 2, 2021
1 parent c6e59e4 commit cc0b436
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/nodesnim/nodes/node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ type
NodeRef* = ref NodeObj


var
handler_default = proc(self: NodeRef) = discard
event_handler_default = proc(self: NodeRef, event: InputEvent) = discard
let
handler_default* = proc(self: NodeRef) = discard
event_handler_default* = proc(self: NodeRef, event: InputEvent) = discard


template nodepattern*(nodetype: untyped): untyped =
Expand Down
6 changes: 4 additions & 2 deletions src/nodesnim/nodescontrol/button.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type
on_touch*: ButtonTouchHandler ## This called, when user clicks on button.
ButtonRef* = ref ButtonObj

let touch_handler = proc(self: ButtonRef, x, y: float) = discard


proc Button*(name: string = "Button"): ButtonRef =
## Creates a new Button node.
Expand All @@ -53,8 +55,8 @@ proc Button*(name: string = "Button"): ButtonRef =
result.normal_background.setColor(Color(0x444444ff))
result.hover_background.setColor(Color(0x505050ff))
result.press_background.setColor(Color(0x595959ff))
result.on_touch = proc(self: ButtonRef, x, y: float) = discard
result.on_text_changed = proc(self: LabelRef, text: string) = discard
result.on_touch = touch_handler
result.on_text_changed = text_changed_handler
result.kind = BUTTON_NODE


Expand Down
16 changes: 9 additions & 7 deletions src/nodesnim/nodescontrol/control.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type
on_unfocus*: ControlHandler ## This called when the Control node loses focus.
ControlRef* = ref ControlObj

let control_handler = proc(self: ControlRef) = discard
let control_xy_handler = proc(self: ControlRef, x, y: float) = discard

template controlpattern*: untyped =
result.hovered = false
Expand All @@ -56,13 +58,13 @@ template controlpattern*: untyped =
result.padding = Anchor(0, 0, 0, 0)
result.background = Drawable()

result.on_mouse_enter = proc(self: ControlRef, x, y: float) = discard
result.on_mouse_exit = proc(self: ControlRef, x, y: float) = discard
result.on_click = proc(self: ControlRef, x, y: float) = discard
result.on_press = proc(self: ControlRef, x, y: float) = discard
result.on_release = proc(self: ControlRef, x, y: float) = discard
result.on_focus = proc(self: ControlRef) = discard
result.on_unfocus = proc(self: ControlRef) = discard
result.on_mouse_enter = control_xy_handler
result.on_mouse_exit = control_xy_handler
result.on_click = control_xy_handler
result.on_press = control_xy_handler
result.on_release = control_xy_handler
result.on_focus = control_handler
result.on_unfocus = control_handler
result.type_of_node = NODE_TYPE_CONTROL

proc Control*(name: string = "Control"): ControlRef =
Expand Down
8 changes: 5 additions & 3 deletions src/nodesnim/nodescontrol/edittext.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type
caret_pos: array[2, uint32]
on_edit*: EditHandler ## This called when user press any key.

let edit_handler = proc(k: string) = discard

const
BLINK_TIME: uint8 = 15
BLINK_WIDTH: float = 2
Expand All @@ -51,8 +53,8 @@ proc EditText*(name: string = "EditText", hint: string = "Edit text ..."): EditT
result.hint.setColor(Color("#ccc"))
result.text.setColor(Color("#555"))
result.text_align = Anchor(0, 0, 0, 0)
result.on_edit = proc(k: string) = discard
result.on_text_changed = proc(self: LabelRef, text: string) = discard
result.on_edit = edit_handler
result.on_text_changed = text_changed_handler
result.kind = EDIT_TEXT_NODE

if result.text.chars.len() > result.hint.chars.len():
Expand Down Expand Up @@ -104,7 +106,7 @@ method draw*(self: EditTextRef, w, h: Glfloat) =
glVertex2f(coords[0], coords[1]-h.Glfloat)
glEnd()
if self.is_blink and i == self.caret_pos[0]:
glColor4f(self.caret_color.r, self.caret_color.g, self.caret_color.b, self.caret_color.a)
glColor(self.caret_color)
glBegin(GL_QUADS)
glVertex2f(coords[0], coords[1])
glVertex2f(coords[0]+BLINK_WIDTH, coords[1])
Expand Down
4 changes: 3 additions & 1 deletion src/nodesnim/nodescontrol/label.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type
text*: StyleText
LabelRef* = ref LabelObj

let text_changed_handler* = proc(self: LabelRef, text: string) = discard


proc Label*(name: string = "Label"): LabelRef =
## Creates a new Label.
Expand All @@ -45,7 +47,7 @@ proc Label*(name: string = "Label"): LabelRef =
result.rect_size.y = 40
result.text = stext""
result.text_align = Anchor(0, 0, 0, 0)
result.on_text_changed = proc(self: LabelRef, text: string) = discard
result.on_text_changed = text_changed_handler
result.kind = LABEL_NODE


Expand Down
3 changes: 2 additions & 1 deletion src/nodesnim/nodescontrol/slider.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type
on_changed*: SliderChangedHandler
SliderRef* = ref SliderObj

let slider_changed_handler = proc(self: SliderRef, new_value: uint) = discard

proc Slider*(name: string = "Slider"): SliderRef =
## Creates a new Slider.
Expand All @@ -45,7 +46,7 @@ proc Slider*(name: string = "Slider"): SliderRef =
result.progress_color = Color(0.5, 0.5, 0.5)
result.max_value = 100
result.value = 0
result.on_changed = proc(self: SliderRef, new_value: uint) = discard
result.on_changed = slider_changed_handler
result.kind = SLIDER_NODE


Expand Down
4 changes: 3 additions & 1 deletion src/nodesnim/nodescontrol/switch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type
on_switch*: SwitchHandler ## This called when switch toggled.
SwitchRef* = ref SwitchObj

let switch_handler = proc(self: SwitchRef, toggled: bool) = discard


proc Switch*(name: string = "Switch"): SwitchRef =
## Creates a new Switch.
Expand All @@ -41,7 +43,7 @@ proc Switch*(name: string = "Switch"): SwitchRef =
result.value = false
result.rect_size.x = 50
result.rect_size.y = 20
result.on_switch = proc(self: SwitchRef, toggled: bool) = discard
result.on_switch = switch_handler
result.kind = COLOR_RECT_NODE


Expand Down
6 changes: 4 additions & 2 deletions src/nodesnim/nodescontrol/texture_button.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type
on_touch*: TextureButtonTouchHandler ## This called, when user clicks on button.
TextureButtonRef* = ref TextureButtonObj

let touch_handler = proc(self: TextureButtonRef, x, y: float) = discard


proc TextureButton*(name: string = "TextureButton"): TextureButtonRef =
## Creates a new TextureButton node.
Expand All @@ -50,8 +52,8 @@ proc TextureButton*(name: string = "TextureButton"): TextureButtonRef =
result.normal_background_texture = GlTextureObj()
result.hover_background_texture = GlTextureObj()
result.press_background_texture = GlTextureObj()
result.on_touch = proc(self: TextureButtonRef, x, y: float) = discard
result.on_text_changed = proc(self: LabelRef, text: string) = discard
result.on_touch = touch_handler
result.on_text_changed = text_changed_handler
result.kind = TEXTURE_BUTTON_NODE


Expand Down
2 changes: 1 addition & 1 deletion src/nodesnim/runtime/scene_loader.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var

mkparse(Node, Scene, AudioStreamPlayer, AnimationPlayer)
mkparse(Control, Box, VBox, HBox, ColorRect, Label, SubWindow, ToolTip,
Button, EditText, TextureButton, TextureRect, GridBox,
Button, EditText, TextureButton, TextureRect, GridBox, CheckBox,
Slider, Switch, Popup, Scroll, Counter, ProgressBar, TextureProgressBar)
mkparse(Node2D, Sprite, AnimatedSprite, KinematicBody2D, CollisionShape2D, TileMap, Camera2D, YSort)
mkparse(Node3D, Sprite3D, GeometryInstance)
Expand Down

0 comments on commit cc0b436

Please sign in to comment.