~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/components/ogre/widgets/Compass.lua

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Compass = {
 
2
connectors={},
 
3
map = nil,
 
4
widget = nil,
 
5
renderImage = nil,
 
6
helper = nil,
 
7
previousPosX = 0,
 
8
previousPosY = 0,
 
9
updateFrameCountDown = -1, --this is used for triggering delayed render updates. If it's more than zero, it's decreased each frame until it's zero, and a render is then carried out. If it's below zero nothing is done.
 
10
zoomInButton = nil,
 
11
anchor = nil
 
12
}
 
13
 
 
14
function Compass.Refresh_Clicked(args)
 
15
        Compass.helper:refresh()
 
16
        Compass.helper:getMap():render()
 
17
        return true
 
18
end
 
19
 
 
20
function Compass.ZoomIn_Clicked(args)
 
21
        local newResolution = Compass.helper:getMap():getResolution() - 0.2
 
22
        --prevent the user from zooming in to much (at which point only one pixel from the head of the avatar will be seen
 
23
        if newResolution > 0.2 then
 
24
                Compass.helper:getMap():setResolution(newResolution)
 
25
                Compass.helper:getMap():render()
 
26
                Compass.helper:refresh()
 
27
        end
 
28
        return true
 
29
end
 
30
function Compass.ZoomOut_Clicked(args)
 
31
        local newResolution = Compass.helper:getMap():getResolution() + 0.2
 
32
        --we'll use the arbitrary resolution of 5 as the max
 
33
        if newResolution < 5 then
 
34
                Compass.helper:getMap():setResolution(Compass.helper:getMap():getResolution() + 0.2)
 
35
                Compass.helper:getMap():render()
 
36
                Compass.helper:refresh()
 
37
        end
 
38
        return true
 
39
end
 
40
 
 
41
 
 
42
 
 
43
function Compass.repositionAtAvatar()
 
44
        local pos = emberOgre:getAvatar():getAvatarSceneNode():getPosition()
 
45
        Compass.helper:reposition(pos.x, pos.z)
 
46
end
 
47
 
 
48
function Compass.framestarted(frameEvent)
 
49
        if Compass.updateFrameCountDown > 0 then
 
50
                Compass.updateFrameCountDown = Compass.updateFrameCountDown - 1
 
51
                if Compass.updateFrameCountDown == 0 then
 
52
                        --if we haven't created any anchor yet, it means that the whole compass is uninitialized and needs to be shown, else we can just rerender the map
 
53
                        if Compass.anchor == nil then
 
54
                                Compass.initialize()
 
55
                        else
 
56
                                Compass.helper:getMap():render()
 
57
                                Compass.helper:refresh()
 
58
                        end
 
59
                        Compass.updateFrameCountDown = -1
 
60
                end
 
61
        end
 
62
end
 
63
 
 
64
function Compass.TerrainPageGeometryUpdated(page)
 
65
        --wait six frames until we rerender the map. This is a hack because apparently the event this listens for doesn't actually guarantee that the page will be rendered next frame. We need to add another event which is emitted when a page actually is rendered the first time.
 
66
        Compass.updateFrameCountDown = 6
 
67
end
 
68
 
 
69
function Compass.initialize()
 
70
        Compass.anchor = EmberOgre.Gui.CompassThirdPersonCameraAnchor:new_local(Compass.helper, emberOgre:getMainCamera():getCamera(), emberOgre:getMainCamera():getRootNode())
 
71
        if Compass.widget ~= nil then
 
72
                Compass.widget:show()
 
73
        end
 
74
end
 
75
 
 
76
function Compass.CreatedAvatarEntity(avatarEntity)
 
77
        connect(Compass.connectors, guiManager.EventFrameStarted, "Compass.framestarted")
 
78
end
 
79
 
 
80
function Compass.buildWidget()
 
81
        Compass.helperImpl = EmberOgre.Gui.RenderedCompassImpl:new_local()
 
82
 
 
83
        Compass.helper = EmberOgre.Gui.Compass:new_local(Compass.helperImpl)
 
84
        Compass.map = Compass.helper:getMap()
 
85
        
 
86
        
 
87
        Compass.buildCEGUIWidget()
 
88
        
 
89
        --don't show the compass here, instead wait until we've gotten some terrain (by listening 
 
90
        connect(Compass.connectors, emberOgre.EventCreatedAvatarEntity, "Compass.CreatedAvatarEntity")
 
91
        connect(Compass.connectors, emberOgre:getTerrainGenerator().EventTerrainPageGeometryUpdated, "Compass.TerrainPageGeometryUpdated")
 
92
 
 
93
end
 
94
 
 
95
-- Call this method to build the cegui widget.
 
96
function Compass.buildCEGUIWidget()
 
97
        Compass.widget = guiManager:createWidget()
 
98
        Compass.widget:loadMainSheet("Compass.layout", "Compass/")
 
99
        Compass.widget:setIsActiveWindowOpaque(false)
 
100
        Compass.renderImage = Compass.widget:getWindow("RenderImage")
 
101
        Compass.pointerImage = Compass.widget:getWindow("Pointer")
 
102
        
 
103
        local assetManager = EmberOgre.Gui.AssetsManager:new_local()
 
104
        
 
105
        --set up the main background image
 
106
        local texturePair = assetManager:createTextureImage(Compass.helperImpl:getTexture(), "CompassMap")
 
107
        if texturePair:hasData() then 
 
108
                Compass.renderImage:setProperty("Image", CEGUI.PropertyHelper:imageToString(texturePair:getTextureImage()))
 
109
        end
 
110
        
 
111
        --also set up the pointer image
 
112
        texturePair = assetManager:createTextureImage(Compass.helperImpl:getPointerTexture(), "CompassPointer")
 
113
        if texturePair:hasData() then 
 
114
                Compass.pointerImage:setProperty("Image", CEGUI.PropertyHelper:imageToString(texturePair:getTextureImage()))
 
115
        end
 
116
        
 
117
        Compass.widget:hide()
 
118
end
 
119
 
 
120
 
 
121
 
 
122
Compass.buildWidget()
 
 
b'\\ No newline at end of file'