-- object_image_display - Logic and behaviors for web image display
-- (component of behavior library)
-- Template attachment: any
-- Created by: Patrick Ferland
-- Created on: 08/22/2008
-- Revision History
-- 08/22/2008 P.Ferland - New script
-- 08/23/2008 P.Ferland - Cleanup and comments
-- 08/29/2008 P.Ferland - Rebuilt from picture_poster to picture_frame and added simplified custom config
-- 09/02/2008 P.Ferland - Added script_load_first support for friendlier OX
-- 10/10/2008 P.Ferland - Added to behavior lib, frames removed for simplicity
-- Listens for these triggers
-- destroyed (cleans up UI)
-- set_script_param (calls picture_poster_update)
-- use (if display not in-world, calls picture_poster_full)
-- attach (calls picture_poster_update)
-- picture_poster_full (launches window display)
-- picture_poster_update (draws UI)
-- Broadcasts these triggers
-- picture_poster_update (to self)
-- picture_poster_full (to self)
-- Constants
PICTURE_FRAME_SCRIPT = '12071:50'
BLANK_IMAGE = '12071:448'
-- Default sizes
TILE_WIDTH = 64
TILE_HEIGHT = 64
IMG_MAX_WIDTH = 160
IMG_MAX_HEIGHT = 120
THUMB =
{
W = 80,
H = 60,
PADDING = 15,
MARGIN = 10,
Y = 30,
}
SAMPLE_IMAGES =
{
['Imagedump: Van Gogh - Starry Night'] =
{
URL = 'http://www.imagedump.com/image.cgi?file=472291.jpg',
W = 350,
H = 280,
},
['Hubble Archive: Antennae Galaxies'] =
{
URL = 'http://imgsrc.hubblesite.org/hu/db/2006/46/images/a/formats/web.jpg',
W = 400,
H = 397,
},
['Wikimedia: Stone Arch in Libya'] =
{
URL = 'http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Libya_5101_Fozzigiaren_Arch_Tadrart_Acacus_Luca_Galuzzi_2007.jpg/800px-Libya_5101_Fozzigiaren_Arch_Tadrart_Acacus_Luca_Galuzzi_2007.jpg',
W = 800,
H = 533,
},
['Wikimedia: Thor'] =
{
URL = 'http://upload.wikimedia.org/wikipedia/commons/thumb/2/23/Thor.jpg/405px-Thor.jpg',
W = 405,
H = 599,
},
['Nasa: Moonscape'] =
{
URL = 'http://cs.jpl.nasa.gov/bergman/ph111.jpg',
W = 738,
H = 560,
},
['Geology.com: Tungurahua volcano'] =
{
URL = 'http://geology.com/news/images/tungurahua-volcano.jpg',
W = 450,
H = 300,
},
['Metaplace: Where Worlds Grow'] =
{
URL = 'http://www.metaplace.com/images/logo_yellow_large.png',
W = 379,
H = 103,
},
['Wikimedia: Sylvilagus Floridanus'] =
{
URL = 'http://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Sylvilagus_floridanus.jpg/600px-Sylvilagus_floridanus.jpg',
W = 600,
H = 600,
},
}
-- Script properties
Define Properties()
-- Friendly script name for OX
script_description = "Image Display"
script_long_description = "This behavior makes an object display an image from the Web when clicked"
script_load_first = 1
image_url = 'http://www.imagedump.com/image.cgi?file=472291.jpg'
ExposeProperty('image_url', 'What is the URL to the image?')
PersistProperty('image_url')
image_width = 350
ExposeProperty('image_width', 'How wide, in pixels, is the original image?')
PersistProperty('image_width')
image_height = 280
ExposeProperty('image_height', 'How high, in pixels, is the original image?')
PersistProperty('image_height')
-- UI handle for image display in-world
image_window = 0
IncludeScript('13197:21') -- ui_library
IncludeScript('12071:3') -- configurable
image_config_tooltips = {}
end
-- Commands
-- no commands
-- Triggers
-- Delete UI if object is destroyed
Trigger destroyed()
if (self.image_window ~= 0) then
UiDelete(self.image_window)
end
end
-- Redraw if properties are changed in OX
Trigger set_script_param(caller, user_id, script_id, name, value)
if (script_id == PICTURE_POSTER_SCRIPT) then
SendTo(self, 'picture_poster_update', 50)
end
end
-- Custom config
Trigger custom_config(caller, user_id, script_id)
if(script_id == PICTURE_FRAME_SCRIPT) then
local display_area = config_window(self, user_id, script_id, 400, 300)
Debug(display_area.id)
-- Set up sample images
local image_index = 0
local image = {}
local image_name = ''
for image_name, image in pairs(SAMPLE_IMAGES) do
local row_index = math.floor(image_index / 4)
local col_index = image_index % 4
local x = col_index * (THUMB.W + THUMB.PADDING) + THUMB.MARGIN
local y = row_index * (THUMB.H + THUMB.PADDING) + THUMB.MARGIN + THUMB.Y
local width = THUMB.W
local height = THUMB.H
local scale = calc_scale(image.W, image.H, width, height)
width = math.floor(image.W * scale)
height = math.floor(image.H * scale)
local img_holder = UiRect(display_area.id, 'picture_frame_thumb_holder_' .. image_index .. '_' .. row_index,
x - 2, y - 2, THUMB.W + 2, THUMB.H + 2)
UiColor(img_holder, 255, 255, 255, 0.4)
local x_off = math.floor( (THUMB.W - width) / 2.0)
local y_off = math.floor( (THUMB.H - height) / 2.0)
local img_ref = UiImageRef(display_area.id, 'picture_frame_thumb_' .. image_index .. '_' .. row_index,
x + x_off, y + y_off, width, height, image.URL)
local select_button = UiImageButton(display_area.id, 'picture_frame_select_' .. image_index .. '_' .. row_index,
x - 2, y - 2, THUMB.W + 2, THUMB.H + 2,
BLANK_IMAGE, BLANK_IMAGE, BLANK_IMAGE,
'/event custom_config_demo_select ' .. self.id .. ' "' .. image_name .. '"')
local tooltip = ui_tooltip(select_button, image_name, 500, 'above', GetObjectById(user_id))
table.insert(self.image_config_tooltips, tooltip)
image_index = image_index + 1
end
local head_label = UiMultiLabel(display_area.id, 'picture_frame_custom_head_label',
10, 10, 320, 24,
0, 0, 0, 255, 255, 255, 0,
0, 0, 0)
UiText(head_label, 'Select one of the images below...')
local foot_label = UiMultiLabel(display_area.id, 'picture_frame_custom_foot_label',
10, 175, 320, 24,
0, 0, 0, 255, 255, 255, 0,
0, 0, 0)
UiText(foot_label, '... or click the "Advanced" button below to show your own!')
end
end
Trigger custom_config_demo_select(user, settings_name)
local image = SAMPLE_IMAGES[settings_name]
self.image_url = image.URL
self.image_width = image.W
self.image_height = image.H
local tooltip = 0
for _, tooltip in ipairs(self.image_config_tooltips) do
UiDelete(tooltip)
end
table.clear(self.image_config_tooltips)
SendTo(self, 'close_ui_all', 0, self, user.id)
end
-- Allow windowed full display
Trigger use(user)
SendTo(self, 'picture_poster_full', 0, user)
end
-- Draw windowed full display
Trigger picture_poster_full(user)
local target_width = 480
local target_height = 360
local scale = calc_scale(self.image_width, self.image_height, target_width, target_height)
local img_width = math.floor(self.image_width * scale)
local img_height = math.floor(self.image_height * scale)
local picture_render = UiImageRef(0, 'picture_render_' .. self.id .. '_' .. user.id,
0, 0, img_width, img_height, self.image_url)
local picture_close_button = UiImageButton(picture_render, 'picture_close_' .. self.id .. '_' .. user.id,
0, 0, img_width, img_height,
BLANK_IMAGE, BLANK_IMAGE, BLANK_IMAGE,
'/event picture_poster_close ' .. self.id .. ' ' .. picture_render)
UiAlign(picture_render, 0, 0, 'center', 'scale_none')
UiAttachUser(user, picture_render)
UiVisible(picture_render, 1)
end
Trigger picture_poster_close(user, picture_id)
local confirm_picture = UiFindWindow(picture_id, 'picture_render_' .. self.id .. '_' .. user.id)
if (picture_id == confirm_picture) then
UiDelete(picture_id)
end
end
-- Local functions
-- Determine appropriate size-limiting scale factor
function calc_scale(width, height, max_width, max_height)
local x_ratio = max_width / width * 1.0
local y_ratio = max_height / height * 1.0
if (x_ratio < y_ratio) then
scale = x_ratio
else
scale = y_ratio
end
return scale
end