Introdução
desenvolvi esse Widget para o HWO, mas resolvi disponibilizar para voces afinal, pode ser util para todos :)
Script
Como instalar?
- Plugin and Play. somente cole em seu editor de script e um script vazio.
Instruções de uso?
- no cabeçalho do Script
- Código:
#----------------------------------------------------------------#
# ** Combo Box [Widget]
#----------------------------------------------------------------#
# Developed: Nanzin
# Date: 23-02-2013 23:30
#----------------------------------------------------------------#
#---------------------------- PT BR -----------------------------#
# Como Usar:
# - Chame a classe em seu código
# => Não permitir onChange (padrão)
# ComboBox.new(window,x,y,options,false)
# => Permitir onChange
# ComboBox.new(window,x,y,options,true){func1;func2}
#
# ONDE:
# Window: Janela que o botão irá aparecer
# x: Coordenada X
# y: Coordenada Y
# options: Hash de opçoes com a arquitetura : {"Opção" => valor}.
# onChange = permitir executar função após alteração de opção?
# como padrão o widget não permite. coloque true se deseja permitir
# {func1;func2}: Funções à serem executadas caso o OnChange seja
# permitido .
#
# Exemplo:
# Não permitir onChange (Padrão)
# @combo = ComboBox.new(self,100,40,{"Nanzin"=>"Programmer","NP"=>"RMXP SDK Online Game"},false).
#
# Permitir OnChange
# @combo = ComboBox.new(self,100,40,{"Nanzin"=>"Programmer","NP"=>"RMXP SDK Online Game"},true){@combo.selectedValue}
#
# Funções do Widget.
# selectedValue: retorna o valor selecionado no combo.
# selectedKey: retorna o nome selecionado no combo.
# setSelected: coloca uma key e seu valor como padrão à
# ser exibido no comboBox.(é necessário a key existir na hash de
# opções para funcionar, caso contrário um aviso será dado
# e a Opção selecionada será escolhida aleatóriamente)
#
#----------------------------- EN [English]-------------------------------
# (I apologize if errors occur in English,
# my English is a little rusty).
#
# How to Use:
# - Call the class in your code
# => Do not allow onChange (default)
# ComboBox.new (window, x, y, options, false)
# => Allow onChange
# ComboBox.new (window, x, y, options, true) {func1, func2}
#
# WHERE:
# Window: Window button that will appear
# X: X coordinate
# Y: Y coordinate
# Options: options hash with architecture: {"Option" => value}.
# OnChange = allow execute function after change option?
# As the default widget does not allow. put true whether to allow
# {Func1, func2}: Functions to be performed if the OnChange is
# Allowed.
#
# Example:
# Do not allow onChange (Standard)
# @combo = ComboBox.new (self, 100.40, {"Nanzin" => "Programmer", "NP" => "RMXP SDK Online Game"}, false).
#
# Allow OnChange
# @combo = ComboBox.new (self, 100.40, {"Nanzin" => "Programmer", "NP" => "RMXP SDK Online Game"}, true) {print(@combo.selectedValue)}
#
# Functions Widget.
# SelectedValue: returns the value selected in the combo.
# SelectedKey: returns the name selected in the combo.
# SetSelected: put a key and its value as the default
# Be displayed in comboBox. (Must exist in the key of hash
# Options to work, otherwise a warning is given
# Option selected and will be chosen randomly)
#----------------------------------------------------------------#
#----------------------------------------------------------------#
class ComboBox < Widget
attr_accessor :options
PROP_KEY = "PROPERTIES" # key properties
TOOLTIP_TXT = "Selected Value:" #tooltip text to exibe
def initialize(win,x,y,options,onChange=false,&onChFunc)
super(win,x,y)
@options = options
@width = (greaterOption * 10)
@height = 18
@onChange = onChange
@onChFunc = onChFunc
@options[PROP_KEY] = {}
# Sprite Base
s.bitmap = Bitmap.new(@width,@height)
s.bitmap.font.color = Color.new(0, 0, 0, 255)
s.bitmap.font.size = win.contents.font.size
#Sprite Button
@bt = Sprite.new(win.viewport)
@bt.bitmap = Bitmap.new(20,18)
@bt.z = 999
# Sprite Button Mask
@bt_mask = Sprite.new(win.viewport)
@bt_mask.bitmap = Bitmap.new(20,18)
@bt_mask.z = 9999
@bt_mask.visible = false
#Sprite Back Options
@back = Sprite.new(win.viewport)
@back.bitmap = Bitmap.new(@s.bitmap.width,@s.bitmap.height * ((@options.size-1).+0.5))
@back.visible = false
@back.z = 9999999
@showingBack = false
# Sprite Mask Option
@mask_opt = Sprite.new(win.viewport)
@mask_opt.bitmap = Bitmap.new(@back.bitmap.width-2,10)
@mask_opt.z = 999999999
@mask_opt.visible = false
@options.each_key {|key| @currentOptionText = key.to_s;break;}
@currentOptionValue = @options[@currentOptionText]
refresh
end
#--------------------------------------------------------------------------
def selectedValue
return @currentOptionValue
end
def selectedKey
return @currentOptionText
end
def greaterOption
gran = 0
@options.each {|key,value|
next if key == PROP_KEY
if key.length > gran
gran = key.length
end
}
return gran
end
def setSelected(selected)
if(@options.has_key?(selected))
changeOption(selected)
else
print("Option not found to define as selected, check your code.")
end
end
#--------------------------------------------------------------------------
def changeOption(opt)
@currentOptionText = opt.to_s
@currentOptionValue = @options[opt]
@back.visible = false
@mask_opt.visible = false
refresh
end
#--------------------------------------------------------------------------
def con(t)
return "" if t == nil
return t if !@hide
return "*"*t.size
end
#--------------------------------------------------------------------------
def refresh
#s.bitmap.clear
s.bitmap.fill_rect(0+1, 0, @s.bitmap.width-2, s.bitmap.height, User_Edit::BORDA)
s.bitmap.fill_rect(0, 0+1, @s.bitmap.width, s.bitmap.height-2, User_Edit::COR1)
s.bitmap.fill_rect(0+1, 0+1, @s.bitmap.width-2, s.bitmap.height-2-10, User_Edit::COR2)
s.bitmap.fill_rect(0+1, 0+1+5, @s.bitmap.width-2, s.bitmap.height-2-10, User_Edit::COR3)
s.bitmap.fill_rect(0+1, 0+1+10, @s.bitmap.width-2, s.bitmap.height-2-10, User_Edit::COR4)
s.bitmap.draw_text(5,0,@width,@height,@currentOptionText)
@bt.bitmap.fill_rect(0+1, 0, @bt.bitmap.width-2, @bt.bitmap.height, User_Edit::BORDA)
@bt.bitmap.fill_rect(0, 0+1, @bt.bitmap.width, @bt.bitmap.height-2, User_Edit::COR1)
@bt.bitmap.fill_rect(0+1, 0+1, @bt.bitmap.width-2, @bt.bitmap.height-2-10, User_Edit::COR2)
@bt.bitmap.fill_rect(0+1, 0+1+5, @bt.bitmap.width-2, @bt.bitmap.height-2-10, User_Edit::COR3)
@bt.bitmap.fill_rect(0+1, 0+1+10, @bt.bitmap.width-2, @bt.bitmap.height-2-10, User_Edit::COR4)
@bt.bitmap.draw_text(5,0,@bt.bitmap.width,@bt.bitmap.height,"▼")
@bt.x = @s.x + @s.bitmap.width
@bt.y = @s.y
@bt_mask.bitmap.fill_rect(0, 0, @bt.bitmap.width, @bt.bitmap.height, Window_Edits::Button_ColorSkin_Start)
@bt_mask.x = @bt.x
@bt_mask.y = @bt.y
@back.bitmap.fill_rect(0+1, 0+1, @back.bitmap.width-2, @back.bitmap.height-2-10, User_Edit::COR3)
@back.x = @s.x
@back.y = @s.y + 17
@mask_opt.bitmap.fill_rect(0, 0, @back.bitmap.width, @back.bitmap.height, Window_Edits::Button_ColorSkin_Start)
x = 3
y = 0
@options.each_key {|key|
next if key == PROP_KEY
@options[PROP_KEY][key] = {"x" => x+32,"y" => y+10,"width" => @s.bitmap.width,"height" => @s.bitmap.height} if x != 3
@options[PROP_KEY][key] = {"x" => x+32,"y" => y+25,"width" => @s.bitmap.width,"height" => @s.bitmap.height} if x == 3
@back.bitmap.draw_text(x,y,@s.bitmap.width,@s.bitmap.height,"#{key}")
y += 15
}
end
#--------------------------------------------------------------------------
def outclick
@back.visible = false
@mask_opt.visible = false
refresh
end
def in_areaDrop?(args=[0,0,@s.bitmap.width,@s.bitmap.height])
min_x=@s.x+args[0]+@win.viewport.rect.x-@win.ox+60
max_x=@s.x + args[2]+args[0]+@win.viewport.rect.x-@win.ox+20
min_y=@s.y+args[1]+@win.viewport.rect.y-@win.oy
max_y=@s.y + args[3]+args[1]+@win.viewport.rect.y-@win.oy
if $Mouse.x.between?(min_x,max_x) and $Mouse.y.between?(min_y,max_y)
return true
end
return false
end
#--------------------------------------------------------------------------
def update
#super
s.visible = win.visible
@bt_mask.visible= false if @bt_mask.visible and !visible
if visible
@bt_mask.x=@bt.x if @bt_mask.x != @bt.x
@bt_mask.y=@bt.y if @bt_mask.y != @bt.y
@bt_mask.update
if in_areaDrop? #@bt.x - 170 , @bt.y - 10
@bt_mask.visible=true
else
@bt_mask.visible=false
end
if in_areaDrop? && Input.pressed?(Input::Mouse_Left) && @back.visible == false #@bt.x - 170 , @bt.y - 10
@back.visible = true
end
@options[PROP_KEY].each_key{|key|
if in_area?([@options[PROP_KEY][key]["x"]-40,@options[PROP_KEY][key]["y"]-10,@options[PROP_KEY][key]["width"],@options[PROP_KEY][key]["height"]]) && @back.visible
@mask_opt.x = @back.x + 1
@mask_opt.y = @back.y + @options[PROP_KEY][key]["y"] - 21
@mask_opt.visible = true
end
if in_area?([@options[PROP_KEY][key]["x"]-40,@options[PROP_KEY][key]["y"]-10,@options[PROP_KEY][key]["width"],@options[PROP_KEY][key]["height"]]) && Input.pressed?(Input::Mouse_Left)
changeOption(key)
if @onChange && @onChFunc != nil then
@onChFunc.call
end
end
}
self.active = false if self.active
self.tool_tip=[TOOLTIP_TXT+" #{selectedValue} "]
check_tool_tip
end
end
#--------------------------------------------------------------------------
def dispose
super
end
end
Mídia
- Spoiler:
- Tooltip com Valor selecionado:
- Video do widget funcionando:
Última edição por Nanzin em Seg Fev 25, 2013 11:37 pm, editado 1 vez(es)