Last active
March 24, 2016 11:38
-
-
Save amirrajan/c36c6c1c8c7c7d63c261 to your computer and use it in GitHub Desktop.
Poor Man's Android View Generation with RM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity < Android::App::Activity | |
include ViewGeneration | |
def generate_screen | |
root_view( | |
horizontal_layout(:row0, text_view(:title, "a dark room")), | |
horizontal_layout(:row1, | |
hidden_button(:outside_button, "a silent forest"), | |
hidden_button(:embark_button, "a dusty path") | |
), | |
progress_bar(:light_fire_pb, "light_fire"), | |
horizontal_layout(:row3, | |
hidden_button(:supplies_button, "supplies"), | |
hidden_button(:builder_button, "builder") | |
), | |
horizontal_layout(:row4, | |
hidden_button(:merchant_button, "merchant"), | |
hidden_button(:workshop_button, "workshop") | |
), | |
spacer() do |spacer_layout| | |
spacer_layout.height = device_height * 0.35 | |
end, | |
history_view | |
) | |
end | |
def history_view | |
horizontal_layout(:history_view, | |
scroll_view(:scroll_view_history, | |
text_view(:text_view_history, "") | |
) | |
) do |horizontal_layout| | |
horizontal_layout.weight = 1 | |
horizontal_layout.setMargins(10, 10, 10, 10) | |
end | |
end | |
def onCreate() | |
setContentView(generate_screen) | |
end | |
def root_view *views | |
r = vertical_layout(:root, *views) | |
r.setBackgroundColor(ViewState.black) | |
r.setDrawingCacheQuality(1) | |
r | |
end | |
def onClick view | |
@views.keys.each do |k| | |
if(@views[k].is_a? Array and @views[k].include? view) | |
self.send(k.to_s + "_clicked") | |
elsif(@views[k] == view) | |
self.send(k.to_s + "_clicked") | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ViewGeneration | |
def button id, text | |
b = Android::Widget::Button.new self | |
b.setText text | |
b.setOnClickListener(self) | |
b.setBackground(button_background) | |
ld = layout_definition( | |
width: :fill, | |
height: :auto, | |
margin_top: 10, | |
weight: 0.3, | |
margin_bottom: 10, | |
margin_right: 10, | |
margin_left: 10) | |
yield ld, b if block_given? | |
b.setLayoutParams(ld) | |
add_to_control_set(id, b) | |
b | |
end | |
def hidden_button id, text | |
raise "hidden_button cannot take in an implicit block" if block_given? | |
button(id, text) { |_, b| hide b } | |
end | |
def text_view id, text | |
tv = Android::Widget::TextView.new(self) | |
tv.setText(text) | |
tv.setGravity(gravity_center) | |
tv.setTextSize(14) | |
ld = layout_definition(width: :fill) | |
yield ld, tv if block_given? | |
tv.setLayoutParams(ld) | |
add_to_control_set(id, tv) | |
tv | |
end | |
def spacer() | |
fl = Android::Widget::FrameLayout.new(self) | |
ld = layout_definition(width: :fill, height: :fill) | |
yield ld if block_given? | |
fl.setLayoutParams(ld) | |
fl | |
end | |
def progress_bar id, text | |
pg = Android::Widget::ProgressBar.new( | |
self, | |
nil, Android::R::Attr::ProgressBarStyleHorizontal) | |
pg.setOnClickListener(self) | |
pg.setClickable(true) | |
pg.setIndeterminate(false) | |
pg.setLayoutParams(layout_definition(width: :fill, margin_left: 5, margin_right: 5, margin_bottom: 5)) | |
tv = text_view(id, text) do |layout| | |
layout.setMargins(0, 5, 0, 0) | |
end | |
tv.setTextSize(18) | |
tv.setOnClickListener(self) | |
add_to_control_set(id, tv) | |
add_to_control_set(id, pg) | |
vl = vertical_layout(id, tv, pg) | |
vl | |
end | |
def vertical_layout id, *views | |
vl = Android::Widget::LinearLayout.new(self) | |
vl.setOrientation(Android::Widget::LinearLayout::VERTICAL) | |
vl.setBaselineAligned(false) | |
vl.setDrawingCacheQuality(1) | |
ld = layout_definition(width: :fill) | |
yield ld, vl if block_given? | |
vl.setLayoutParams(ld) | |
views.each { |v| vl.addView(v) } | |
add_to_control_set(id, vl) | |
vl | |
end | |
def horizontal_layout id, *views | |
hl = Android::Widget::LinearLayout.new(self) | |
hl.setOrientation(Android::Widget::LinearLayout::HORIZONTAL) | |
hl.setBaselineAligned(false) | |
hl.setDrawingCacheQuality(1) | |
ld = layout_definition(width: :fill) | |
yield ld, hl if block_given? | |
hl.setLayoutParams(ld) | |
views.each { |v| hl.addView(v) } | |
add_to_control_set(id, hl) | |
hl | |
end | |
def relative_layout id, *views | |
rl = Android::Widget::RelativeLayout.new(self) | |
# rl.setOrientation(Android::Widget::LinearLayout::HORIZONTAL) | |
# rl.setBaselineAligned(false) | |
rl.setDrawingCacheQuality(1) | |
ld = layout_definition(width: :fill, height: :fill) | |
yield ld, rl if block_given? | |
rl.setLayoutParams(ld) | |
views.each { |v| rl.addView(v) } | |
add_to_control_set(id, rl) | |
rl | |
end | |
def scroll_view id, *views | |
sv = Android::Widget::ScrollView.new(self) | |
sv.setLayoutParams(layout_definition(width: :fill)) | |
sv.setSmoothScrollingEnabled(true) | |
sv.setFillViewport(true) | |
ld = layout_definition(width: :fill) | |
yield ld if block_given? | |
sv.setLayoutParams(ld) | |
views.each { |v| sv.addView(v) } | |
add_to_control_set id, sv | |
sv | |
end | |
def layout_definition opts | |
attrs = { | |
fill: fill, | |
auto: auto | |
} | |
width = attrs[opts[:width] || :auto] | |
height = attrs[opts[:height] || :auto] || opts[:height] | |
left = opts[:margin_left] || 0 | |
top = opts[:margin_top] || 0 | |
right = opts[:margin_right] || 0 | |
bottom = opts[:margin_bottom] || 0 | |
layout = Android::Widget::LinearLayout::LayoutParams.new(width, height, opts[:weight] || 0) | |
layout.setMargins(left, top, right, bottom) | |
layout | |
end | |
def resolve_type type | |
@lookup ||= { | |
button: Android::Widget::Button, | |
progress_bar: Android::Widget::ProgressBar, | |
text_view: Android::Widget::TextView, | |
scroll_view: Android::Widget::ScrollView, | |
linear_layout: Android::Widget::LinearLayout | |
} | |
@lookup[type] || type | |
end | |
def find_view id, type = Android::View::View | |
@find_view_cache ||= { } | |
type = resolve_type(type) | |
return @find_view_cache[[type, id]] if @find_view_cache[[type, id]] | |
if(@views[id].is_a? Array) | |
@find_view_cache[[type, id]] = @views[id].find { |c| c.is_a? type } | |
else | |
@find_view_cache[[type, id]] = @views[id] | |
end | |
@find_view_cache[[type, id]] | |
end | |
def find_views_of_type type | |
@find_views_cache ||= { } | |
type = resolve_type(type) | |
return @find_views_cache[type] if @find_views_cache[type] | |
results = [] | |
@views.keys.each do |k| | |
if @views[k].is_a? Array | |
@views[k].each do |v| | |
results << v if v.is_a? type | |
end | |
elsif @views[k].is_a? type | |
results << @views[k] | |
end | |
end | |
@find_views_cache[type] = results | |
@find_views_cache[type] | |
end | |
def add_to_control_set id, control | |
if(@views[id] and @views[id].is_a? Array) | |
@views[id] << control | |
elsif(@views[id]) | |
all = Array.new | |
all = [@views[id], control] | |
@views[id] = all | |
else | |
@views[id] = control | |
end | |
end | |
def pb_disabled_color | |
@pb_disabled_color ||= Android::Graphics::LightingColorFilter.new("FF000000".hex, "FF666666".hex) | |
@pb_disabled_color | |
end | |
def fill | |
Android::Widget::LinearLayout::LayoutParams::MATCH_PARENT | |
end | |
def auto | |
Android::Widget::LinearLayout::LayoutParams::WRAP_CONTENT | |
end | |
def gravity_left | |
Android::View::Gravity::LEFT | |
end | |
def gravity_center | |
Android::View::Gravity::CENTER | |
end | |
def hide view | |
view.setVisibility(0x0000000004) | |
end | |
def gone view | |
view.setVisibility(0x0000000008) | |
end | |
def fade_in view, animate | |
return if view.getVisibility() == 0x0000000000 | |
if animate | |
view.setAlpha(0) | |
view.setVisibility(0x0000000000) | |
a = Android::Animation::ObjectAnimator.new | |
a.target = view | |
a.propertyName = "alpha" | |
a.setObjectValues([0, 1]) | |
a.setEvaluator(Android::Animation::FloatEvaluator.new) | |
a.setDuration(1000) | |
a.start | |
else | |
view.setAlpha(100) | |
view.setVisibility(0x0000000000) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment