Skip to content
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 numeric input #110

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
16 changes: 14 additions & 2 deletions src/mruby-widget-lib/mrblib/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,14 @@ def handleCursorPos(x,y,mod)
end

def handleScroll(x, y, scroll)
aw = activeWidget(x, y, :onScroll)
aw.onScroll scroll if(aw.respond_to? :onScroll)

if @modal && @modal.respond_to?(:handleScroll)
#~ puts("@modal.handleScroll")
@modal.handleScroll(x, y, scroll)
else
aw = activeWidget(x, y, :onScroll)
aw.onScroll scroll if(aw.respond_to? :onScroll)
end
end

def quit
Expand Down Expand Up @@ -332,6 +338,12 @@ def key_mod(press, key)

def key(key, act)
aw = nil

if @modal && @modal.respond_to?(:onKey)
@modal.onKey(key, act)
return
end

if @keyboard
aw = activeWidget(@keyboard.x, @keyboard.y, :onKey)
end
Expand Down
211 changes: 211 additions & 0 deletions src/mruby-zest/example/NumericInput.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
Widget {
id: numeric
property Object valueRef: nil
property Symbol style: :normal
property Function whenValue: nil
property Float value: 0
property bool first: true

function onSetup(old=nil)
{
@last = Time.new
}

function animate()
{
if(root.key_widget != self)
@next = nil
if(@state)
@state = nil
damage_self
end
return
end
now = Time.new
if(@next.nil?)
@next = now + 0.1
return
elsif(@next < now)
@state = !@state
@next = now + 0.7
damage_self
end
}

function draw(vg)
{
if(self.style == :overlay)
draw_overlay(vg)
else
draw_normal(vg)
end

}

function draw_overlay(vg)
{
background color("1b1c1c")
vg.font_face("bold")
vg.font_size h*0.8
vg.text_align NVG::ALIGN_LEFT | NVG::ALIGN_MIDDLE
vg.fill_color = color("56c0a5")
l = "..."
if(label.class == String && !label.empty?)
l = label.clone
elsif(label.class == Array)
l = label[0].clone
end

(0...l.length).each do |i|
l[i] = "?" if l.getbyte(i) > 127
end

bnd = vg.text_bounds(0,0,l+"|")
if(bnd+8 > self.w)
vg.font_size self.h*self.w/(bnd+8)*0.8
bnd = vg.text_bounds(0,0,l)
end

vg.text(8,h/2,l)
if(@state)
vg.text(8+bnd,h/2,"|")
end
}
function draw_normal(vg)
{
background Theme::GeneralBackground
vg.font_face("bold")
vg.font_size h*0.8
vg.text_align NVG::ALIGN_LEFT | NVG::ALIGN_MIDDLE
vg.fill_color = Theme::TextColor
l = label.empty? ? "..." : label.clone

(0...l.length).each do |i|
l[i] = "?" if l.getbyte(i) > 127
end

vg.path do
vg.rect(1, 1, w-2, h-2)
vg.stroke_color color("3AC5EC")
vg.stroke_width 1.0
vg.stroke
end

vg.text(8,h/2,l)
bnd = vg.text_bounds(0,0,l)
if(@state)
vg.text(8+bnd,h/2,"|")
end
}


function handleScroll(x, y, ev)
{

glb_x = parent.global_x + self.x
glb_y = parent.global_y + self.y + self.h/2
dx = x - glb_x
dy = y - glb_y

if (y > parent.global_y + self.y && y < parent.global_y + self.y + self.h )

ind = (((dx - 10) / 13)).floor
ind = 0 if (ind < 0)

ind_colon = label.index('.')

if (ind_colon)
if (ind<ind_colon)
exponent = ind_colon-ind-1
elsif (ind>ind_colon)
exponent = ind_colon-ind
end

else
exponent = label[/\A\d*/].length - ind -1
exponent = 0 if(exponent < 0)
end
increment = (10 ** exponent)*ev.dy
if self.parent.type
value = self.label.to_f
else
value = self.label.to_i
end

value = value + increment

$remote.setf(self.parent.extern, value) if(parent.valueRef)
parent.whenValue.call if parent.whenValue
parent.damage_self
self.label = value.to_s
self.damage_self
self.first = false

end

}
function onKey(k, mode)
{
return if mode != "press"
if(k.ord == 27) #esc
self.label = ""
whenEnter
return
elsif(k.ord == 13) #enter
whenEnter
return
elsif(k.ord == 8) #backspace

self.label = self.label[0...-1] if !self.label.empty?
self.damage_self
self.first = false
return
elsif k.ord >= 44 && k.ord <= 57 # numbers OR , . -
if (self.first)
self.label = ""
self.first = false
self.label = self.label + k
else
self.label = self.label + k if k != '-' # '-' only at the beginning
end



self.damage_self
return
end
damage_self
}

function whenEnter() {
if whenValue then
whenValue.call
else
self.root.ego_death self
end

}

function onMousePress(m)
{
now = Time.new
@last ||= now
whenEnter if((now-@last)>0.05)
}



function onMerge(val)
{
self.label = val.label
}

onExtern: {
ref = OSC::RemoteParam.new($remote, numeric.extern)
ref.callback = lambda {|x|
numeric.label = x;
numeric.damage_self
}
numeric.valueRef = ref
}
}
2 changes: 1 addition & 1 deletion src/mruby-zest/qml/NumEntry.qml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ Widget {
Theme::ButtonGrad1, Theme::ButtonGrad2)
vg.fill_paint paint
vg.fill
vg.stroke_width 1
vg.stroke_width 2
vg.stroke
end

Expand Down
43 changes: 42 additions & 1 deletion src/mruby-zest/qml/Valuator.qml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,46 @@ Widget {

}

function numeric_input()
{

widget = Qml::NumericInput.new(valuator.db)
widget.w = (self.type) ? 160 : 80
widget.h = 40
widget.x = (valuator.w-widget.w)/2
widget.y = -valuator.h/2
widget.layer = 2
if(self.type)
value = valuator.valueRef.display_value.round(5)
else
value = valuator.valueRef.display_value
end
widget.label = value.to_s
widget.whenValue = lambda {

root.set_modal(nil)
old_dsp = valuator.valueRef.display_value
numericString = widget.label.gsub(',', '.')
if(valuator.valueRef)
$remote.setf(self.extern, numericString.to_f) if self.type and !(widget.label.empty?)
$remote.seti(self.extern, numericString.to_i) if !self.type and !(widget.label.empty?)
new_dsp = valuator.valueRef.display_value
whenValue.call if whenValue && (new_dsp.nil? || old_dsp != new_dsp)
out_value = displayValueToText(valuator.valueRef.display_value)
valuator.root.log(:user_value, out_value, src=valuator.label)
else
whenValue.call if whenValue
end
damage_self

}
root.set_modal(widget)
Qml::add_child(valuator, widget)
valuator.root.smash_draw_seq
valuator.root.damage_item widget
}


//Callback function which does not propagate info to remote API
function setValue(v) {
if(valuator.valueRef.has_logmin() && v > 0.0 && v < 0.07)
Expand Down Expand Up @@ -126,7 +166,8 @@ Widget {
@click_time = now
elsif(ev.buttons.include? :rightButton)
if(children.empty?)
create_radial
#create_radial
numeric_input()
end
elsif(ev.buttons.include? :middleButton)
reset
Expand Down