• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

AdvancedCameraControl Example

Status
Not open for further replies.

MindWorX

Tool Moderator
Level 20
Joined
Aug 3, 2004
Messages
709
JASS:
library AdvancedCameraControl initializer Init
    globals
        //Settings
        private real    real_ZoomMinDistance    = 0.00      //Min Zoom Distance
        private real    real_ZoomMaxDistance    = 3000.00   //Max Zoom Distance
        private real    real_ZoomDistance       = 1700.00   //Initial Zoom Distance
        private real    real_ZoomDistanceChange = 256.00    //Step per wheel scroll step
        private real    real_AoA                = 304.00    //Initial Angle
        private real    real_Rot                = 90.00     //Initial Direction
        
        //System
        private constant trigger trig_OnMouseLeftDown   = CreateTrigger()
        private constant trigger trig_OnMouseLeftUp     = CreateTrigger()
        private constant trigger trig_OnMouseRightDown  = CreateTrigger()
        private constant trigger trig_OnMouseRightUp    = CreateTrigger()
        private boolean bool_MouseLeftState             = false
        private boolean bool_MouseRightState            = false
        
        private constant trigger trig_OnMouseWheel      = CreateTrigger()
        private constant trigger trig_AoAControl        = CreateTrigger()
        private boolean bool_AoAChanging                = false
        private integer int_AoAOffsetY                  = GetMouseY()
        private integer int_AoAOffsetX                  = GetMouseX()
    endglobals
    
        //=== Set Left Mouse Button Down State
    private function OnMouseLeftDown takes nothing returns nothing
        set bool_MouseLeftState = true
    endfunction
    
        //=== Set Left Mouse Button Up State
    private function OnMouseLeftUp takes nothing returns nothing
        set bool_MouseLeftState = false
        set bool_AoAChanging    = false
    endfunction
    
        //=== Set Right Mouse Button Down State
    private function OnMouseRightDown takes nothing returns nothing
        set bool_MouseRightState = true
    endfunction
    
        //=== Set Right Mouse Button Up State
    private function OnMouseRightUp takes nothing returns nothing
        set bool_MouseRightState    = false
        set bool_AoAChanging        = false
    endfunction
    
    function GetMouseLeftState takes nothing returns boolean
        return bool_MouseLeftState
    endfunction
    
    function GetMouseRightState takes nothing returns boolean
        return bool_MouseRightState
    endfunction
    
    private function AoAControl takes nothing returns nothing
     local boolean b = GetMouseRightState() and GetMouseLeftState()
        if(b) then
            set bool_AoAChanging = true
            set int_AoAOffsetY  = GetMouseY()
            set int_AoAOffsetX  = GetMouseX()
        endif
    endfunction
    
        //=== Zooming
    private function OnMouseWheel takes nothing returns nothing
     local integer WheelDelta   = GetTriggerWheelDelta()
     local real zoomchange      = real_ZoomDistanceChange * (ISignBJ(WheelDelta) * -1)
        set real_ZoomDistance   = real_ZoomDistance + zoomchange
        if(real_ZoomDistance < real_ZoomMinDistance) then
            set real_ZoomDistance = real_ZoomMinDistance
        endif
        if(real_ZoomDistance > real_ZoomMaxDistance) then
            set real_ZoomDistance = real_ZoomMaxDistance
        endif
    endfunction

    function CameraControl takes nothing returns nothing
     local real offset = 0
        call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, real_ZoomDistance, 1.00)
        if(bool_AoAChanging) then
            set offset          = (int_AoAOffsetY - GetMouseY())
            set real_AoA        = real_AoA + offset
            set int_AoAOffsetY  = GetMouseY()
        endif
        if(real_AoA > 350) then
            set real_AoA = 350
        endif
        if(real_AoA < 270) then
            set real_AoA = 270
        endif
        call SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK, real_AoA, 0.00)
        if(bool_AoAChanging) then
            set offset          = (int_AoAOffsetX - GetMouseX())
            set real_Rot        = real_Rot + offset
            set int_AoAOffsetX  = GetMouseX()
        endif
        if(real_Rot > 360) then
            set real_Rot = real_Rot - 360
        endif
        if(real_Rot < 0) then
            set real_Rot = real_Rot + 360
        endif
        call SetCameraField(CAMERA_FIELD_ROTATION, real_Rot, 0.00)
    endfunction

    private function Init takes nothing returns nothing
     local trigger t = CreateTrigger()
        
        call TriggerRegisterTimerEvent(t, 0.01, true)
        call TriggerAddAction(t, function CameraControl)
        
        call TriggerRegisterMouseEvent(trig_OnMouseLeftDown, EVENT_LMOUSEDOWN)
        call TriggerAddAction(trig_OnMouseLeftDown, function OnMouseLeftDown)
        
        call TriggerRegisterMouseEvent(trig_OnMouseLeftUp, EVENT_LMOUSEUP)
        call TriggerAddAction(trig_OnMouseLeftUp, function OnMouseLeftUp)
        
        call TriggerRegisterMouseEvent(trig_OnMouseRightDown, EVENT_RMOUSEDOWN)
        call TriggerAddAction(trig_OnMouseRightDown, function OnMouseRightDown)
        
        call TriggerRegisterMouseEvent(trig_OnMouseRightUp, EVENT_RMOUSEUP)
        call TriggerAddAction(trig_OnMouseRightUp, function OnMouseRightUp)
        
        call TriggerRegisterMouseEvent(trig_OnMouseWheel, EVENT_MOUSEWHEEL)
        call TriggerAddAction(trig_OnMouseWheel, function OnMouseWheel)
        
        call TriggerRegisterMouseEvent(trig_AoAControl, EVENT_LMOUSEDOWN)
        call TriggerRegisterMouseEvent(trig_AoAControl, EVENT_RMOUSEDOWN)
        call TriggerAddAction(trig_AoAControl, function AoAControl)
    endfunction
endlibrary

Uncommented atm, but it's pretty straight forward. It uses a vJass library setup, so it requires NewGen to be used in a map.

It gives more control of the camera, allowing you to move the angle of attack and direction by holding down both the left and right mouse button, and allows much better zoom.
 
Last edited:
Status
Not open for further replies.
Top