From 0c4730dffc5ce3dbbb39997ab8469fd9e53e0616 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 18 Dec 2023 21:33:59 -0800 Subject: make button backgrounds optional --- button.lua | 4 +++- reference.md | 19 +++++++++---------- source_text.lua | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/button.lua b/button.lua index 36923e2..c3b7110 100644 --- a/button.lua +++ b/button.lua @@ -9,7 +9,9 @@ -- draw button and queue up event handlers function button(State, name, params) - love.graphics.setColor(params.bg.r, params.bg.g, params.bg.b, params.bg.a) + if params.bg then + love.graphics.setColor(params.bg.r, params.bg.g, params.bg.b, params.bg.a) + end love.graphics.rectangle('fill', params.x,params.y, params.w,params.h, 5,5) if params.icon then params.icon(params) end table.insert(State.button_handlers, params) diff --git a/reference.md b/reference.md index cd79c49..705194e 100644 --- a/reference.md +++ b/reference.md @@ -277,22 +277,21 @@ The following facilities help set these things up: * `button` creates a single button. The syntax is: ``` - button(state, name, {x=..., y=..., w=..., h=..., color={r,g,b}, + button(state, name, {x=..., y=..., w=..., h=..., bg={r,g,b}, icon = function({x=..., y=..., w=..., h=...}) ... end, onpress1 = ... }) ``` - Call this either directly or indirectly from `App.draw`. It will paint a - rectangle to the screen with top-left at (x,y), dimensions w×h pixels in the - specified `color`. It will then overlay any drawing instructions within - `icon` atop it. The `icon` callback will receive a table containing the same - x/y/w/h. + Call this either directly or indirectly from `App.draw`. It will assign a + rectangle with the given dimensions and trigger the provided (zero-arg) + `onpress1` callback when the primary mouse button is clicked within. + It will also optionally paint the rectangle with the specified background + color `bg` and a foreground described by the `icon` callback (which will + receive the same dimensions). - The rectangle also registers within `state` the `onpress1` callback (without - any arguments) when mouse button 1 is clicked on it. This way you can see - everything about a button in one place. Create as many buttons as you like - within a single shared `state`. + This way you can see everything about a button in one place. Create as many + buttons as you like within a single shared `state`. * `mouse_press_consumed_by_any_button(state, x,y, mouse_button)` diff --git a/source_text.lua b/source_text.lua index dac2820..a1c7e6c 100644 --- a/source_text.lua +++ b/source_text.lua @@ -34,7 +34,7 @@ function Text.draw(State, line_index, y, startpos, hide_cursor, show_line_number local s,e,filename = unpack(link_offsets) local lo, hi = Text.clip_wikiword_with_screen_line(line, line_cache, i, s, e) if lo then - button(State, 'link', {x=State.left+lo, y=y, w=hi-lo, h=State.line_height, bg={r=1,g=1,b=1}, + button(State, 'link', {x=State.left+lo, y=y, w=hi-lo, h=State.line_height, icon = icon.hyperlink_decoration, onpress1 = function() if file_exists(filename) then -- cgit 1.4.1-2-gfad0 From 81883d7dcaad04bcc335043db45b2f6fb2b16801 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 18 Dec 2023 21:39:01 -0800 Subject: bugfix :( --- button.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/button.lua b/button.lua index c3b7110..df83704 100644 --- a/button.lua +++ b/button.lua @@ -11,8 +11,8 @@ function button(State, name, params) if params.bg then love.graphics.setColor(params.bg.r, params.bg.g, params.bg.b, params.bg.a) + love.graphics.rectangle('fill', params.x,params.y, params.w,params.h, 5,5) end - love.graphics.rectangle('fill', params.x,params.y, params.w,params.h, 5,5) if params.icon then params.icon(params) end table.insert(State.button_handlers, params) end -- cgit 1.4.1-2-gfad0