UIControls can be disabled, added blacklist to TextBox control

This commit is contained in:
Maruno17
2023-11-29 23:39:10 +00:00
parent 973b93a524
commit 5553218507
13 changed files with 116 additions and 69 deletions

View File

@@ -6,20 +6,22 @@
#
#===============================================================================
class UIControls::BaseControl < BitmapSprite
attr_reader :value
# attr_accessor :disabled # TODO: Make use of this.
attr_reader :value
attr_accessor :disabled
TEXT_COLOR = Color.black
TEXT_SIZE = 18 # Default is 22 if size isn't explicitly set
HOVER_COLOR = Color.cyan # For clickable area when hovering over it
CAPTURE_COLOR = Color.pink # For area you clicked in but aren't hovering over
TEXT_COLOR = Color.black
TEXT_SIZE = 18 # Default is 22 if size isn't explicitly set
HOVER_COLOR = Color.cyan # For clickable area when hovering over it
CAPTURE_COLOR = Color.pink # For area you clicked in but aren't hovering over
DISABLED_COLOR = Color.gray
DISABLED_COLOR_DARK = Color.new(128, 128, 128)
def initialize(width, height, viewport)
super(width, height, viewport)
self.bitmap.font.color = TEXT_COLOR
self.bitmap.font.size = TEXT_SIZE
# @disabled = false # TODO: Make use of this.
@disabled = false
@hover_area = nil # Is a symbol from the keys for @interactions if the mouse is hovering over that interaction
@captured_area = nil # Is a symbol from the keys for @interactions (or :none) if this control is clicked in
clear_changed
@@ -58,6 +60,22 @@ class UIControls::BaseControl < BitmapSprite
#-----------------------------------------------------------------------------
def disabled?
return @disabled
end
def disable
return if disabled?
@disabled = true
invalidate
end
def enable
return if !disabled?
@disabled = false
invalidate
end
def invalid?
return @invalid
end
@@ -177,8 +195,7 @@ class UIControls::BaseControl < BitmapSprite
# Updates the logic on the control, invalidating it if necessary.
def update
return if !self.visible
# TODO: Disabled control stuff.
# return if self.disabled
return if disabled? && !busy?
update_hover_highlight

View File

@@ -33,6 +33,12 @@ class UIControls::Checkbox < UIControls::BaseControl
def refresh
super
# Draw disabled colour
if disabled?
self.bitmap.fill_rect(@checkbox_rect.x, @checkbox_rect.y,
@checkbox_rect.width, @checkbox_rect.height,
DISABLED_COLOR)
end
# Draw checkbox outline
self.bitmap.outline_rect(@checkbox_rect.x, @checkbox_rect.y,
@checkbox_rect.width, @checkbox_rect.height,
@@ -40,12 +46,12 @@ class UIControls::Checkbox < UIControls::BaseControl
# Draw checkbox fill
if @value # If checked
self.bitmap.fill_rect(@checkbox_rect.x + @checkbox_rect.width - CHECKBOX_FILL_SIZE - 2, @checkbox_rect.y + 2,
CHECKBOX_FILL_SIZE, CHECKBOX_FILL_SIZE, CHECKED_COLOR)
CHECKBOX_FILL_SIZE, CHECKBOX_FILL_SIZE, (disabled?) ? DISABLED_COLOR_DARK : CHECKED_COLOR)
self.bitmap.outline_rect(@checkbox_rect.x + @checkbox_rect.width - CHECKBOX_FILL_SIZE - 2, @checkbox_rect.y + 2,
CHECKBOX_FILL_SIZE, CHECKBOX_FILL_SIZE, self.bitmap.font.color)
else
self.bitmap.fill_rect(@checkbox_rect.x + 2, @checkbox_rect.y + 2,
CHECKBOX_FILL_SIZE, CHECKBOX_FILL_SIZE, UNCHECKED_COLOR)
CHECKBOX_FILL_SIZE, CHECKBOX_FILL_SIZE, (disabled?) ? DISABLED_COLOR_DARK : UNCHECKED_COLOR)
self.bitmap.outline_rect(@checkbox_rect.x + 2, @checkbox_rect.y + 2,
CHECKBOX_FILL_SIZE, CHECKBOX_FILL_SIZE, self.bitmap.font.color)
end

View File

@@ -4,8 +4,6 @@
# decide which characters are selected. Maybe? Note that this method
# is only triggered upon the initial mouse press, and isn't repeated
# while it's still held down.
# TODO: Add a blacklist array. Can't type in any values in this array. Disable
# this control if @value is in this array.
#===============================================================================
class UIControls::TextBox < UIControls::BaseControl
TEXT_BOX_X = 2
@@ -21,6 +19,7 @@ class UIControls::TextBox < UIControls::BaseControl
@display_pos = 0
@cursor_timer = nil
@cursor_shown = false
@blacklist = []
end
def value=(new_value)
@@ -53,6 +52,11 @@ class UIControls::TextBox < UIControls::BaseControl
invalidate
end
def set_blacklist(*list)
@blacklist = list
invalidate
end
def set_interactive_rects
@text_box_rect = Rect.new(TEXT_BOX_X, (height - TEXT_BOX_HEIGHT) / 2,
[TEXT_BOX_WIDTH, width].min, TEXT_BOX_HEIGHT)
@@ -63,6 +67,12 @@ class UIControls::TextBox < UIControls::BaseControl
#-----------------------------------------------------------------------------
def disabled?
val = (@value.respond_to?("strip!")) ? @value.strip : @value
return true if @blacklist.include?(val)
return super
end
def busy?
return @cursor_pos >= 0 if @captured_area == :text_box
return super
@@ -157,6 +167,12 @@ class UIControls::TextBox < UIControls::BaseControl
def refresh
super
# Draw disabled colour
if disabled?
self.bitmap.fill_rect(@text_box_rect.x, @text_box_rect.y,
@text_box_rect.width, @text_box_rect.height,
DISABLED_COLOR)
end
# Draw text box outline
self.bitmap.outline_rect(@text_box_rect.x, @text_box_rect.y,
@text_box_rect.width, @text_box_rect.height,
@@ -204,6 +220,7 @@ class UIControls::TextBox < UIControls::BaseControl
invalidate
else
@value.strip! if @value.respond_to?("strip!")
@value = @initial_value if disabled?
set_changed if @initial_value && @value != @initial_value
reset_interaction
end
@@ -224,6 +241,7 @@ class UIControls::TextBox < UIControls::BaseControl
# Released mouse button outside of text box, or initially clicked outside of
# text box; end interaction with this control
@value.strip! if @value.respond_to?("strip!")
@value = @initial_value if disabled?
set_changed if @initial_value && @value != @initial_value
reset_interaction
super # Make this control not busy again
@@ -252,6 +270,7 @@ class UIControls::TextBox < UIControls::BaseControl
if Input.triggerex?(:RETURN) || Input.repeatex?(:RETURN) ||
Input.triggerex?(:KP_ENTER) || Input.repeatex?(:KP_ENTER)
@value.strip! if @value.respond_to?("strip!")
@value = @initial_value if disabled?
set_changed if @initial_value && @value != @initial_value
reset_interaction
@captured_area = nil
@@ -274,8 +293,6 @@ class UIControls::TextBox < UIControls::BaseControl
def update
return if !self.visible
super
# TODO: Disabled control stuff.
# return if self.disabled
# Make the cursor flash
if @captured_area == :text_box
cursor_to_show = ((System.uptime - @cursor_timer) / 0.35).to_i.even?

View File

@@ -1,5 +1,6 @@
#===============================================================================
#
# TODO: Is there a better knob design than a big black rectangle? I'd rather
# it not be a different colour.
#===============================================================================
class UIControls::NumberSlider < UIControls::BaseControl
attr_reader :min_value
@@ -15,10 +16,6 @@ class UIControls::NumberSlider < UIControls::BaseControl
VALUE_X = PLUS_X + PLUS_MINUS_SIZE + 5
TEXT_OFFSET_Y = 5
# TODO: Is there a better knob design than a big black rectangle? I'd rather
# it not be a different colour.
SLIDER_KNOB_COLOR = Color.black
def initialize(width, height, viewport, min_value, max_value, value)
super(width, height, viewport)
@min_value = min_value
@@ -72,8 +69,9 @@ class UIControls::NumberSlider < UIControls::BaseControl
def refresh
super
button_color = (disabled?) ? DISABLED_COLOR : self.bitmap.font.color
# Draw minus button
self.bitmap.fill_rect(@minus_rect.x + 2, @minus_rect.y + (@minus_rect.height / 2) - 2, @minus_rect.width - 4, 4, self.bitmap.font.color)
self.bitmap.fill_rect(@minus_rect.x + 2, @minus_rect.y + (@minus_rect.height / 2) - 2, @minus_rect.width - 4, 4, button_color)
# Draw slider bar
self.bitmap.fill_rect(SLIDER_X, (self.height / 2) - 1, SLIDER_LENGTH, 2, self.bitmap.font.color)
# Draw notches on slider bar
@@ -83,10 +81,10 @@ class UIControls::NumberSlider < UIControls::BaseControl
# Draw slider knob
fraction = (self.value - self.min_value) / (self.max_value.to_f - self.min_value)
knob_x = (SLIDER_LENGTH * fraction).to_i
self.bitmap.fill_rect(SLIDER_X + knob_x - 4, (self.height / 2) - 6, 8, 12, SLIDER_KNOB_COLOR)
self.bitmap.fill_rect(SLIDER_X + knob_x - 4, (self.height / 2) - 6, 8, 12, button_color)
# Draw plus button
self.bitmap.fill_rect(@plus_rect.x + 2, @plus_rect.y + (@plus_rect.height / 2) - 2, @plus_rect.width - 4, 4, self.bitmap.font.color)
self.bitmap.fill_rect(@plus_rect.x + (@plus_rect.width / 2) - 2, @plus_rect.y + 2, 4, @plus_rect.height - 4, self.bitmap.font.color)
self.bitmap.fill_rect(@plus_rect.x + 2, @plus_rect.y + (@plus_rect.height / 2) - 2, @plus_rect.width - 4, 4, button_color)
self.bitmap.fill_rect(@plus_rect.x + (@plus_rect.width / 2) - 2, @plus_rect.y + 2, 4, @plus_rect.height - 4, button_color)
# Draw value text
draw_text(self.bitmap, VALUE_X, TEXT_OFFSET_Y, self.value.to_s)
end
@@ -108,8 +106,6 @@ class UIControls::NumberSlider < UIControls::BaseControl
def update
return if !self.visible
super
# TODO: Disabled control stuff.
# return if self.disabled
case @captured_area
when :minus
# Constant decrement of value while pressing the minus button

View File

@@ -78,11 +78,12 @@ class UIControls::NumberTextBox < UIControls::TextBox
def refresh
super
button_color = (disabled?) ? DISABLED_COLOR : self.bitmap.font.color
# Draw minus button
self.bitmap.fill_rect(@minus_rect.x + 2, @minus_rect.y + (@minus_rect.height / 2) - 2, @minus_rect.width - 4, 4, self.bitmap.font.color)
self.bitmap.fill_rect(@minus_rect.x + 2, @minus_rect.y + (@minus_rect.height / 2) - 2, @minus_rect.width - 4, 4, button_color)
# Draw plus button
self.bitmap.fill_rect(@plus_rect.x + 2, @plus_rect.y + (@plus_rect.height / 2) - 2, @plus_rect.width - 4, 4, self.bitmap.font.color)
self.bitmap.fill_rect(@plus_rect.x + (@plus_rect.width / 2) - 2, @plus_rect.y + 2, 4, @plus_rect.height - 4, self.bitmap.font.color)
self.bitmap.fill_rect(@plus_rect.x + 2, @plus_rect.y + (@plus_rect.height / 2) - 2, @plus_rect.width - 4, 4, button_color)
self.bitmap.fill_rect(@plus_rect.x + (@plus_rect.width / 2) - 2, @plus_rect.y + 2, 4, @plus_rect.height - 4, button_color)
end
#-----------------------------------------------------------------------------

View File

@@ -50,6 +50,12 @@ class UIControls::Button < UIControls::BaseControl
def refresh
super
# Draw disabled colour
if disabled?
self.bitmap.fill_rect(@button_rect.x, @button_rect.y,
@button_rect.width, @button_rect.height,
DISABLED_COLOR)
end
# Draw button outline
self.bitmap.outline_rect(@button_rect.x, @button_rect.y,
@button_rect.width, @button_rect.height,

View File

@@ -5,6 +5,7 @@
# any lag at the moment with a tall list.
# TODO: Make a viewport for the list, and allow scrolling positions halfway
# through a line? Nah.
# TODO: This control cannot be disabled.
#===============================================================================
class UIControls::List < UIControls::BaseControl
LIST_X = 0
@@ -217,8 +218,6 @@ class UIControls::List < UIControls::BaseControl
return if !self.visible
@scrollbar.update
super
# TODO: Disabled control stuff.
# return if self.disabled
# Refresh the list's position if changed by moving the scrollbar
self.top_row = (@scrollbar.position.to_f / ROW_HEIGHT).round
# Set the selected row to the row the mouse is over, if clicked on

View File

@@ -81,10 +81,16 @@ class UIControls::DropdownList < UIControls::BaseControl
def refresh
@dropdown_menu&.refresh
super
# Draw disabled colour
if disabled?
self.bitmap.fill_rect(@button_rect.x, @button_rect.y,
@button_rect.width, @button_rect.height,
DISABLED_COLOR)
end
# Draw button outline
self.bitmap.outline_rect(@button_rect.x, @button_rect.y,
@button_rect.width, @button_rect.height,
Color.black)
self.bitmap.font.color)
# Draw value
draw_text(self.bitmap, @button_rect.x + TEXT_BOX_PADDING, TEXT_OFFSET_Y, @options[@value] || "???")
# Draw down arrow
@@ -95,7 +101,7 @@ class UIControls::DropdownList < UIControls::BaseControl
6.times do |i|
self.bitmap.fill_rect(arrow_area_x + (arrow_area_width / 2) - 5 + i,
@button_rect.y + (arrow_area_width / 2) - 1 + i,
11 - (2 * i), 1, Color.black)
11 - (2 * i), 1, (disabled?) ? DISABLED_COLOR_DARK : self.bitmap.font.color)
end
end

View File

@@ -87,6 +87,7 @@ class UIControls::Scrollbar < UIControls::BaseControl
if @captured_area == :slider || (!@captured_area && @hover_area == :slider)
bar_color = GRAB_COLOR
end
bar_color = DISABLED_COLOR if disabled?
self.bitmap.fill_rect(@slider.x, @slider.y, @slider.width, @slider.height, bar_color)
end
end
@@ -120,8 +121,6 @@ class UIControls::Scrollbar < UIControls::BaseControl
def update
return if !self.visible
super
# TODO: Disabled control stuff.
# return if self.disabled
if @captured_area == :slider
# TODO: Have a display y position for the slider bar which is in pixels,
# and round it to the nearest row when setting @top_row? This is
@@ -141,7 +140,7 @@ class UIControls::Scrollbar < UIControls::BaseControl
self.slider_top = @slider_top + ((@tray_size - @slider_size) / 4.0).ceil
end
end
else
elsif !disabled?
mouse_x, mouse_y = mouse_pos
if mouse_x && mouse_y && @interactions[:slider_tray].contains?(mouse_x, mouse_y)
wheel_v = Input.scroll_v