-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add scrollbar.horizontal
#74
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,15 @@ function SCROLLBAR.scroll_to(scrollbar, x, y) | |
local adjusted_height = (scrollbar.bounds_size.y - scrollbar.handle_size.y) | ||
local offset_y = scrollbar.vertical and (scrollbar.handle_size.y / 2) or 0 | ||
handle_pos.y = offset_y + adjusted_height - y * adjusted_height | ||
scrollbar.scroll.y = y | ||
else | ||
local adjusted_width = (scrollbar.bounds_size.x - scrollbar.handle_size.x) | ||
local offset_x = scrollbar.vertical and 0 or (scrollbar.handle_size.x / 2) | ||
handle_pos.x = offset_x + adjusted_width - x * adjusted_width | ||
scrollbar.scroll.x = x | ||
end | ||
gui.set_position(scrollbar.node, handle_pos) | ||
scrollbar.scroll.y = y | ||
scrollbar.refresh() | ||
britzl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
function SCROLLBAR.set_visible(scrollbar, visible) | ||
gui.set_enabled(scrollbar.node, visible) | ||
|
@@ -54,14 +56,13 @@ function M.vertical(handle_id, bounds_id, action_id, action, fn, refresh_fn) | |
|
||
scrollbar.enabled = core.is_enabled(handle) | ||
scrollbar.node = handle | ||
scrollbar.node_id = node_id | ||
scrollbar.node_id = handle_id | ||
scrollbar.refresh_fn = refresh_fn | ||
scrollbar.bounds = bounds | ||
scrollbar.bounds_size = bounds_size | ||
scrollbar.handle_size = handle_size | ||
|
||
if action then | ||
scrollbar.refresh_fn = refresh_fn | ||
britzl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
local action_pos = vmath.vector3(action.x, action.y, 0) | ||
|
||
core.clickable(scrollbar, action_id, action) | ||
|
@@ -81,6 +82,48 @@ function M.vertical(handle_id, bounds_id, action_id, action, fn, refresh_fn) | |
return scrollbar | ||
end | ||
|
||
function M.horizontal(handle_id, bounds_id, action_id, action, fn, refresh_fn) | ||
handle_id = core.to_hash(handle_id) | ||
bounds_id = core.to_hash(bounds_id) | ||
local handle = gui.get_node(handle_id) | ||
local bounds = gui.get_node(bounds_id) | ||
assert(handle) | ||
assert(bounds) | ||
local scrollbar = core.instance(handle_id, scrollbars, SCROLLBAR) | ||
scrollbar.scroll = scrollbar.scroll or vmath.vector3(0, 0, 0) | ||
scrollbar.vertical = false | ||
|
||
local handle_size = gui.get_size(handle) | ||
local bounds_size = gui.get_size(bounds) | ||
|
||
scrollbar.enabled = core.is_enabled(handle) | ||
scrollbar.node = handle | ||
scrollbar.node_id = handle_id | ||
scrollbar.refresh_fn = refresh_fn | ||
scrollbar.bounds = bounds | ||
scrollbar.bounds_size = bounds_size | ||
scrollbar.handle_size = handle_size | ||
|
||
if action then | ||
local action_pos = vmath.vector3(action.x, action.y, 0) | ||
|
||
core.clickable(scrollbar, action_id, action) | ||
if scrollbar.pressed_now or scrollbar.pressed then | ||
local adjusted_x, adjusted_y = core.adjust(gui.get_adjust_mode(handle), action_pos.x, action_pos.y) | ||
local bounds_pos = core.get_root_position(bounds) | ||
local size = bounds_size.x - handle_size.x | ||
local ratio = (size - (adjusted_x - bounds_pos.x - (scrollbar.handle_size.x / 2))) / size | ||
SCROLLBAR.scroll_to(scrollbar, ratio, 0) | ||
fn(scrollbar) | ||
end | ||
else | ||
SCROLLBAR.scroll_to(scrollbar, scrollbar.scroll.x, 0) | ||
end | ||
|
||
scrollbar.refresh() | ||
return scrollbar | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: This part is mostly copy-pasted from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is perfectly fine to be honest. Sure, the first 20 lines or so could be moved to a shared function perhaps, but lets keep it as it is! |
||
|
||
setmetatable(M, { | ||
__call = function(_, ...) | ||
return M.scrollbar(...) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thought: I don't know what these are about, but the editor added it to all components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, this is a new thing introduced with custom GUI nodes, such as Spine nodes from the Spine extension.