It doesn't look like there is a concept of a button in DragonRuby Game Toolkit. How do I create UI components such as buttons?
It doesn't look like there is a concept of a button in DragonRuby Game Toolkit. How do I create UI components such as buttons?
Buttons (and any other UI component) can be deconstructed into primitives:
The destruction of the button into the parts above yields the following result:
def tick args
# create an entity in state that represents the button
args.state.click_me_button.border ||= { x: 10, y: 10, w: 100, h: 50 }
args.state.click_me_button.label ||= { x: 10, y: 10, text: "click me" }
# render the button
args.outputs.borders << args.state.click_me_button.border
args.outputs.labels << args.state.click_me_button.label
# determine if the button has been clicked
if (args.inputs.mouse.click) &&
(args.inputs.mouse.point.inside_rect? args.state.click_me_button.border)
args.gtk.notify! "button was clicked"
end
end
If you want to get fancy, the button code above can be generalized and placed into a function:
# helper method to create a button
def new_button id, x, y, text
# create a hash ("entity") that has some metadata
# about what it represents
entity = {
id: id,
rect: { x: x, y: y, w: 100, h: 50 }
}
# for that entity, define the primitives
# that form it
entity[:primitives] = [
{ x: x, y: y, w: 100, h: 50 }.border,
{ x: x, y: y, text: text }.label
]
entity
end
# helper method for determining if a button was clicked
def button_clicked? args, button
return false unless args.inputs.mouse.click
return args.inputs.mouse.point.inside_rect? button[:rect]
end
def tick args
# use helper method to create a button
args.state.click_me_button ||= new_button :click_me, 10, 10, "click me"
# render button generically using `args.outputs.primitives`
args.outputs.primitives << args.state.click_me_button[:primitives]
# check if the click occurred using the button_clicked? helper method
if button_clicked? args, args.state.click_me_button
args.gtk.notify! "click me button was clicked!"
end
end