daily pastebin goal
44%
SHARE
TWEET

Untitled

a guest Feb 22nd, 2016 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Terrain Editor UI
  2.  
  3. TerrainEditUI=ScriptObject()
  4.  
  5. uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  6. ui.root.defaultStyle = uiStyle;
  7. iconStyle = cache:GetResource("XMLFile", "UI/EditorIcons.xml");
  8.  
  9. function CreateCursor()
  10.     local cursor = Cursor:new("Cursor")
  11.     --cursor.defaultStyle=uiStyle
  12.     --cursor.style=AUTO_STYLE
  13.     cursor:SetStyleAuto(uiStyle)
  14.     cursor:SetPosition(graphics.width / 2, graphics.height / 2)
  15.     ui.cursor = cursor
  16.     cursor.visible=true
  17.  
  18. end
  19.  
  20. function TerrainEditUI:Start()
  21.     self.heightbrush=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditHeightBrush.xml"))
  22.     self.blendbrush=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditBlendBrush.xml"))
  23.     self.maskbrush=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditMaskBrush.xml"))
  24.     self.smoothbrush=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditSmoothBrush.xml"))
  25.     self.newterrain=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditNewTerrain.xml"))
  26.     self.toolbar=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditToolbar.xml"))
  27.     self.filterui=ui:LoadLayout(cache:GetResource("XMLFile", "UI/TerrainEditFilters.xml"))
  28.     self.filterlist=self.filterui:GetChild("FilterList", true)
  29.     self.filteroptions=self.filterui:GetChild("FilterOptions", true)
  30.    
  31.     local content=Window:new(context)
  32.     content.style=uiStyle
  33.     self.filteroptions.contentElement=content
  34.    
  35.    
  36.    
  37.    
  38.     self.heightbrush.style=uiStyle
  39.     self.blendbrush.style=uiStyle
  40.     self.maskbrush.style=uiStyle
  41.     self.newterrain.style=uiStyle
  42.     self.toolbar.style=uiStyle
  43.     self.smoothbrush.style=uiStyle
  44.     self.filterui.style=uiStyle
  45.    
  46.     self.brushpreview=Image(context)
  47.     self.brushpreview:SetSize(64,64,3)
  48.     self.brushtex=Texture2D:new(context)
  49.     self.brushtex:SetSize(0,0,0,TEXTURE_DYNAMIC)
  50.    
  51.     self.heightbrush:GetChild("BrushPreview",true).texture=self.brushtex
  52.     self.blendbrush:GetChild("BrushPreview",true).texture=self.brushtex
  53.     self.maskbrush:GetChild("BrushPreview",true).texture=self.brushtex
  54.     self.smoothbrush:GetChild("BrushPreview",true).texture=self.brushtex
  55.    
  56.     ui.root:AddChild(self.heightbrush)
  57.     ui.root:AddChild(self.blendbrush)
  58.     ui.root:AddChild(self.maskbrush)
  59.     ui.root:AddChild(self.smoothbrush)
  60.     ui.root:AddChild(self.newterrain)
  61.     ui.root:AddChild(self.toolbar)
  62.     ui.root:AddChild(self.filterui)
  63.    
  64.    
  65.     self.mode=0
  66.     self.blendbrush.visible=false
  67.     self.maskbrush.visible=false
  68.     self.newterrain.visible=false
  69.     self.smoothbrush.visible=false
  70.     self.filterui.visible=false
  71.     self.toolbar.visible=true
  72.    
  73.    
  74.     self:SubscribeToEvent("Pressed", "TerrainEditUI:HandleButtonPress")
  75.     self:SubscribeToEvent("SliderChanged", "TerrainEditUI:HandleSliderChanged")
  76.     self:SubscribeToEvent("Toggled", "TerrainEditUI:HandleToggled")
  77.     self:SubscribeToEvent("ItemSelected", "TerrainEditUI:HandleItemSelected")
  78.    
  79.     self.brushcursornode=scene_:CreateChild()
  80.     self.brushcursor=self.brushcursornode:CreateComponent("CustomGeometry")
  81.     self.brushcursor:SetNumGeometries(1)
  82.     self.brushmat=cache:GetResource("Material", "Materials/TerrainBrush.xml")
  83.     self.brushmat:SetTexture(0, self.brushtex)
  84.     self.brushcursor:SetMaterial(self.brushmat)
  85.    
  86.     self.waypointpreview=scene_:CreateComponent("CustomGeometry")
  87.     self.waypointpreview:SetNumGeometries(1)
  88.     self.waypointpreviewmaterial=cache:GetResource("Material", "Materials/WaypointPreview.xml")
  89.     self.waypointpreview:SetMaterial(self.waypointpreviewmaterial)
  90.    
  91.     self.mode=0
  92.     self:ActivateHeightBrush()
  93.    
  94.     self:PopulateFilterList()
  95.    
  96.     self.counter=0
  97.     -- Waypoints
  98.     waypoints={}
  99. end
  100.  
  101. function TerrainEditUI:UpdateWaypointVis()
  102.     --print("1")
  103.     self.waypointpreview:Clear()
  104.     self.waypointpreview.occludee=false
  105.     self.waypointpreview:SetNumGeometries(1)
  106.     local c
  107.     local spacing=terrain:GetSpacing()
  108.     local plist=RasterVertexList()
  109.     for _,c in ipairs(waypoints) do
  110.         local pos=c.position
  111.         local norm=WorldToNormalized(hmap,terrain,pos)
  112.         local hx=math.floor(norm.x*hmap:GetWidth())
  113.         local hy=math.floor(norm.y*hmap:GetHeight())
  114.         local ht=GetHeightValue(hmap,hx,(hmap:GetHeight()-1)-hy)
  115.         plist:push_back(RasterVertex(hx,hy,ht))
  116.     end
  117.    
  118.     if plist:size()<4 then return end
  119.     --print("Num waypoints: "..plist:size())
  120.     local curve=RasterVertexList()
  121.     TessellateLineList(plist, curve, 10)
  122.     --print("Num curve points: "..curve:size())
  123.     local quad=RasterVertexList()
  124.     BuildQuadStrip(curve, quad, 8)
  125.     --print("Num quad points: "..quad:size())
  126.    
  127.     self.waypointpreview:BeginGeometry(0,TRIANGLE_LIST)
  128.     self.waypointpreview:SetDynamic(true)
  129.    
  130.     function buildVertex(rv)
  131.         local nx=rv.x_/hmap:GetWidth()
  132.         local ny=rv.y_/hmap:GetHeight()
  133.         local v=NormalizedToWorld(hmap,terrain,Vector2(nx,ny))
  134.         v.y=(rv.val_*255)*spacing.y
  135.         return v
  136.     end
  137.    
  138.     for c=0,quad:size()-4,2 do
  139.         self.waypointpreview:DefineVertex(buildVertex(quad:at(c)))
  140.         self.waypointpreview:DefineVertex(buildVertex(quad:at(c+1)))
  141.         self.waypointpreview:DefineVertex(buildVertex(quad:at(c+2)))
  142.        
  143.         self.waypointpreview:DefineVertex(buildVertex(quad:at(c+1)))
  144.         self.waypointpreview:DefineVertex(buildVertex(quad:at(c+2)))
  145.         self.waypointpreview:DefineVertex(buildVertex(quad:at(c+3)))
  146.         --print("hi")
  147.        
  148.     end
  149.    
  150.     self.waypointpreview:Commit()
  151.     self.waypointpreview:SetMaterial(self.waypointpreviewmaterial)
  152.     local bbox=self.waypointpreview.worldBoundingBox
  153.     bbox:Define(Vector3(-1000,-1000,-1000), Vector3(1000,1000,1000))
  154. end
  155.  
  156. function TerrainEditUI:AddWaypoint(groundx, groundz)
  157.     local waynode=scene_:CreateChild()
  158.     local model=waynode:CreateComponent("StaticModel")
  159.     model.material=cache:GetResource("Material", "Materials/Flag.xml")
  160.     model.model=cache:GetResource("Model", "Models/Flag.mdl")
  161.     model.castShadows=false
  162.     local ht=terrain:GetHeight(Vector3(groundx,0,groundz))
  163.     waynode.position=Vector3(groundx, ht, groundz)
  164.     waynode.scale=Vector3(0.25,0.25,0.25)
  165.     table.insert(waypoints, waynode)
  166.     self:UpdateWaypointVis()
  167. end
  168.  
  169. function TerrainEditUI:BuildFilterOptions(filter)
  170.     if filter==nil then return end
  171.     local options=self.filteroptions:GetChild("OptionsWindow", true)
  172.     local name=self.filteroptions:GetChild("FilterName", true)
  173.     local desc=self.filteroptions:GetChild("FilterDescription", true)
  174.     name.text=filter.name
  175.     desc.text=filter.description
  176.    
  177.     options:RemoveAllChildren()
  178.    
  179.     if filter.options==nil then print("No options") return end
  180.     local c
  181.     local maxx,maxy=0,0
  182.     for _,c in ipairs(filter.options) do
  183.         print("Option: "..c.name)
  184.         local window=Window:new(context)
  185.         window.defaultStyle=uiStyle
  186.         window.style=uiStyle
  187.         window.layoutMode=LM_HORIZONTAL
  188.         window.layoutBorder=IntRect(5,5,5,5)
  189.         local title=Text:new(context)
  190.         title.text=c.name
  191.         title.defaultStyle=uiStyle
  192.         title.style=uiStyle
  193.         --title.maxSize=IntVector2(64,0)
  194.         window:AddChild(title)
  195.        
  196.         if c.type=="flag" then
  197.             local check=CheckBox:new(context)
  198.             check.name=c.name
  199.             check.defaultStyle=uiStyle
  200.             check.style=uiStyle
  201.             if c.value==true then check.checked=true
  202.             else check.checked=false
  203.             end
  204.             window:AddChild(check)
  205.             window.size=IntVector2(title.size.x+check.size.x, 15)
  206.         elseif c.type=="value" then
  207.             local edit=LineEdit:new(context)
  208.             edit.name=c.name
  209.             edit.defaultStyle=uiStyle
  210.             edit.style=uiStyle
  211.             edit.textElement.text=tostring(c.value)
  212.             window:AddChild(edit)
  213.             window.size=IntVector2(title.size.x+edit.size.x, 15)
  214.         end
  215.         --if window.size.x > maxx then maxx=window.size.x end
  216.         window.maxSize=IntVector2(10000,25)
  217.         options:AddChild(window)
  218.     end
  219.    
  220.     --options.size.x=maxx
  221.     self.filteroptions.visible=true
  222. end
  223.  
  224. function TerrainEditUI:PopulateFilterList()
  225.     self.filters={}
  226.     self.selectedfilter=nil
  227.     local options=self.filteroptions:GetChild("OptionsWindow", true)
  228.     options:RemoveAllChildren()
  229.    
  230.     local list=self.filterlist:GetChild("List", true)
  231.     if list==nil then return end
  232.     list:RemoveAllItems()
  233.    
  234.     local filters=fileSystem:ScanDir(fileSystem:GetProgramDir().."/TerrainEditFilters", "*.lua", SCAN_FILES, false)
  235.     if filters==nil then print("Uh oh")
  236.     else
  237.         local c
  238.         for _,c in ipairs(filters) do
  239.             local filter=dofile("TerrainEditFilters/"..c)
  240.             print(c)
  241.             self.filters[filter.name]=filter
  242.             local uielement=Text:new(context)
  243.             uielement.style="EditorEnumAttributeText"
  244.             uielement.text=filter.name
  245.             uielement.name=filter.name
  246.             list:AddItem(uielement)
  247.         end
  248.     end
  249. end
  250.  
  251. function TerrainEditUI:HandleItemSelected(eventType, eventData)
  252.     local which=eventData["ListView"]:GetPtr("Element")
  253.     local selected=eventData:GetInt("Selection")
  254.     local entry=which:GetItem(selected)
  255.     if entry==nil then return end
  256.     local name=entry:GetName()
  257.    
  258.     if self.filters[name]==nil then return end
  259.    
  260.     self:BuildFilterOptions(self.filters[name])
  261.     self.selectedfilter=self.filters[name]
  262.    
  263.     --if self.filters[name] then
  264.         --self.filters[name]:execute()
  265.     --end
  266. end
  267.  
  268. function TerrainEditUI:GetBrushSettings(brush)
  269.     local power,max,radius,hardness=0,0,5,0.9
  270.     local usemask=false
  271.    
  272.     local slider
  273.     slider=brush:GetChild("PowerSlider", true)
  274.     if slider then power=(slider.value/slider.range)*4 end
  275.    
  276.     slider=brush:GetChild("MaxSlider", true)
  277.     if slider then max=(slider.value/slider.range) end
  278.    
  279.     slider=brush:GetChild("RadiusSlider", true)
  280.     if slider then radius=math.floor((slider.value/slider.range)*30) end
  281.    
  282.     slider=brush:GetChild("HardnessSlider", true)
  283.     if slider then hardness=(slider.value/slider.range) end
  284.    
  285.     local button=brush:GetChild("MaskCheck", true)
  286.     if button then usemask=button.checked end
  287.    
  288.     return power,max,radius,math.min(1,hardness),usemask
  289. end
  290.  
  291. function TerrainEditUI:BuildCursorMesh(radius)
  292.     self.brushcursor:BeginGeometry(0,TRIANGLE_LIST)
  293.     self.brushcursor:SetDynamic(true)
  294.     --self.brushcursor:SetMaterial(0, self.brushmat)
  295.    
  296.     local spacing=terrain:GetSpacing()
  297.     local spacingx=spacing.x
  298.     local spacingz=spacing.z
  299.     local meshsize=math.floor(radius)*2+2
  300.     local originx=(-meshsize/2)*spacingx
  301.     local originz=(-meshsize/2)*spacingx
  302.    
  303.     local uvspacing=1/(meshsize-1)
  304.    
  305.     local x,z
  306.     for x=0,meshsize-2,1 do
  307.         for z=0,meshsize-2,1 do
  308.             self.brushcursor:DefineVertex(Vector3(originx+x*spacingx, 0, originz+z*spacingz))
  309.             self.brushcursor:DefineTexCoord(Vector2(x*uvspacing, z*uvspacing))
  310.            
  311.             self.brushcursor:DefineVertex(Vector3(originx+(x+1)*spacingx, 0, originz+(z+1)*spacingz))
  312.             self.brushcursor:DefineTexCoord(Vector2((x+1)*uvspacing, (z+1)*uvspacing))
  313.            
  314.             self.brushcursor:DefineVertex(Vector3(originx+x*spacingx, 0, originz+(z+1)*spacingz))
  315.             self.brushcursor:DefineTexCoord(Vector2(x*uvspacing, (z+1)*uvspacing))
  316.            
  317.             self.brushcursor:DefineVertex(Vector3(originx+x*spacingx, 0, originz+z*spacingz))
  318.             self.brushcursor:DefineTexCoord(Vector2(x*uvspacing, z*uvspacing))
  319.            
  320.             self.brushcursor:DefineVertex(Vector3(originx+(x+1)*spacingx, 0, originz+z*spacingz))
  321.             self.brushcursor:DefineTexCoord(Vector2((x+1)*uvspacing, z*uvspacing))
  322.            
  323.             self.brushcursor:DefineVertex(Vector3(originx+(x+1)*spacingx, 0, originz+(z+1)*spacingz))
  324.             self.brushcursor:DefineTexCoord(Vector2((x+1)*uvspacing, (z+1)*uvspacing))
  325.         end
  326.     end
  327.    
  328.     self.brushcursor:Commit()
  329.    
  330.     self.brushcursor:SetMaterial(0, self.brushmat)
  331. end
  332.  
  333. function TerrainEditUI:SetBrushCursorHeight()
  334.     local mousepos
  335.     if input.mouseVisible then
  336.         mousepos=input:GetMousePosition()
  337.     else
  338.         mousepos=ui:GetCursorPosition()
  339.     end
  340.    
  341.     local ground=cam:GetScreenGround(mousepos.x, mousepos.y)
  342.    
  343.     --[[local numverts=self.brushcursor:GetNumVertices(0)
  344.    
  345.     local v
  346.     for v=0,numverts-1,1 do
  347.         local vert=self.brushcursor:GetVertex(0,v).position
  348.         local ht=terrain:GetHeight(Vector3(vert.x+ground.x,0,vert.z+ground.z))
  349.         vert.y=ht
  350.     end
  351.    
  352.     self.brushcursor:Commit()]]
  353.     SetBrushCursorHeight(terrain, self.brushcursor, ground.x, ground.z)
  354. end
  355.  
  356. function TerrainEditUI:ActivateHeightBrush()
  357.     self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.heightbrush)
  358.    
  359.     self:BuildCursorMesh(self.radius)
  360.     self:GenerateBrushPreview(self.hardness)
  361.     self.heightbrush:GetChild("BrushPreview",true):SetTexture(self.brushtex)
  362.     self.heightbrush:GetChild("BrushPreview",true):SetImageRect(IntRect(0,0,63,63))
  363.     self.heightbrush.visible=true
  364.     self.blendbrush.visible=false
  365.     self.maskbrush.visible=false
  366.     self.smoothbrush.visible=false
  367.    
  368.     self.activebrush=self.heightbrush
  369.    
  370.     local text=self.activebrush:GetChild("PowerText", true)
  371.     if text then text.text=string.format("%.1f", self.power) end
  372.     text=self.activebrush:GetChild("RadiusText", true)
  373.     if text then text.text=tostring(math.floor(self.radius)) end
  374.     text=self.activebrush:GetChild("MaxText", true)
  375.     if text then text.text=string.format("%.1f", self.max) end
  376.     text=self.activebrush:GetChild("HardnessText", true)
  377.     if text then text.text=string.format("%.2f", self.hardness) end
  378.    
  379. end
  380.  
  381. function TerrainEditUI:ActivateBlendBrush()
  382.     self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.blendbrush)
  383.    
  384.     self:BuildCursorMesh(self.radius)
  385.     self:GenerateBrushPreview(self.hardness)
  386.     self.blendbrush:GetChild("BrushPreview",true):SetTexture(self.brushtex)
  387.     self.blendbrush:GetChild("BrushPreview",true):SetImageRect(IntRect(0,0,63,63))
  388.     self.heightbrush.visible=false
  389.     self.blendbrush.visible=true
  390.     self.maskbrush.visible=false
  391.     self.smoothbrush.visible=false
  392.    
  393.     self.activebrush=self.blendbrush
  394.     local text=self.activebrush:GetChild("PowerText", true)
  395.     if text then text.text=string.format("%.1f", self.power) end
  396.     text=self.activebrush:GetChild("RadiusText", true)
  397.     if text then text.text=tostring(math.floor(self.radius)) end
  398.     text=self.activebrush:GetChild("MaxText", true)
  399.     if text then text.text=string.format("%.1f", self.max) end
  400.     text=self.activebrush:GetChild("HardnessText", true)
  401.     if text then text.text=string.format("%.2f", self.hardness) end
  402. end
  403.  
  404. function TerrainEditUI:ActivateSmoothBrush()
  405.     self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.smoothbrush)
  406.    
  407.     self:BuildCursorMesh(self.radius)
  408.     self:GenerateBrushPreview(self.hardness)
  409.     self.blendbrush:GetChild("BrushPreview",true):SetTexture(self.brushtex)
  410.     self.blendbrush:GetChild("BrushPreview",true):SetImageRect(IntRect(0,0,63,63))
  411.     self.heightbrush.visible=false
  412.     self.blendbrush.visible=false
  413.     self.maskbrush.visible=false
  414.     self.smoothbrush.visible=true
  415.    
  416.     self.activebrush=self.smoothbrush
  417.     local text=self.activebrush:GetChild("PowerText", true)
  418.     if text then text.text=string.format("%.1f", self.power) end
  419.     text=self.activebrush:GetChild("RadiusText", true)
  420.     if text then text.text=tostring(math.floor(self.radius)) end
  421.     text=self.activebrush:GetChild("MaxText", true)
  422.     if text then text.text=string.format("%.1f", self.max) end
  423.     text=self.activebrush:GetChild("HardnessText", true)
  424.     if text then text.text=string.format("%.2f", self.hardness) end
  425. end
  426.  
  427. function TerrainEditUI:ActivateMaskBrush()
  428.     self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.maskbrush)
  429.    
  430.     self:BuildCursorMesh(self.radius)
  431.     self:GenerateBrushPreview(self.hardness)
  432.     self.blendbrush:GetChild("BrushPreview",true):SetTexture(self.brushtex)
  433.     self.blendbrush:GetChild("BrushPreview",true):SetImageRect(IntRect(0,0,63,63))
  434.     self.heightbrush.visible=false
  435.     self.blendbrush.visible=false
  436.     self.maskbrush.visible=true
  437.     self.newterrain.visible=false
  438.     self.smoothbrush.visible=false
  439.    
  440.     self.activebrush=self.maskbrush
  441.     local text=self.activebrush:GetChild("PowerText", true)
  442.     if text then text.text=string.format("%.1f", self.power) end
  443.     text=self.activebrush:GetChild("RadiusText", true)
  444.     if text then text.text=tostring(math.floor(self.radius)) end
  445.     text=self.activebrush:GetChild("MaxText", true)
  446.     if text then text.text=string.format("%.1f", self.max) end
  447.     text=self.activebrush:GetChild("HardnessText", true)
  448.     if text then text.text=string.format("%.2f", self.hardness) end
  449. end
  450.  
  451.  
  452.  
  453. function TerrainEditUI:Update(dt)
  454.     self.counter=self.counter+dt
  455.     if self.counter>4 then
  456.         self.counter=self.counter-4
  457.         print("Used mem: "..collectgarbage("count"))
  458.         collectgarbage()
  459.     end
  460.     local mousepos
  461.     if input.mouseVisible then
  462.         mousepos=input:GetMousePosition()
  463.     else
  464.         mousepos=ui:GetCursorPosition()
  465.     end
  466.    
  467.     local ground=cam:GetScreenGround(mousepos.x, mousepos.y)
  468.    
  469.     if ground then
  470.         local world=Vector3(ground.x,0,ground.z)
  471.         self.brushcursornode:SetPosition(world)
  472.    
  473.         self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.activebrush)
  474.    
  475.         self:SetBrushCursorHeight()
  476.     end
  477.  
  478.    
  479.     if input:GetMouseButtonDown(MOUSEB_LEFT) and ui:GetElementAt(mousepos.x, mousepos.y)==nil then
  480.         -- If CTRL is down and we are in mode==0 then grab the terrain height at the cursor instead
  481.         if self.mode==0 and input:GetQualifierDown(QUAL_CTRL) then
  482.             local ground=cam:PickGround(mousepos.x, mousepos.y)
  483.             if ground~=nil then
  484.                 local norm=WorldToNormalized(hmap,terrain,ground)
  485.                 --local tx=math.floor(norm.x*hmap:GetWidth()-1)
  486.                 --local ty=math.floor(norm.y*hmap:GetHeight()-1)
  487.                 --local col=hmap:GetPixel(tx,hmap:GetHeight()-ty)
  488.                 local col=hmap:GetPixelBilinear(norm.x,1-norm.y)
  489.                 local ht=0
  490.                 if hmap.components==1 then ht=col.r
  491.                 else ht=col.r+col.g/256.0
  492.                 end
  493.                 print(ht)
  494.                
  495.                 local slider=self.activebrush:GetChild("MaxSlider", true)
  496.                 if slider then slider.value=ht*slider.range end
  497.                 self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.activebrush)
  498.                 --self:BuildCursorMesh(self.radius)
  499.                 self:GenerateBrushPreview(self.hardness)
  500.             end
  501.         else
  502.             local ground=cam:GetScreenGround(mousepos.x, mousepos.y)
  503.             if ground~=nil then
  504.                 local gx,gz=ground.x,ground.z
  505.            
  506.                 --self.edit:ApplyBrush(gx,gz, self.radius, self.max, self.power, self.hardness, self.mode, self.usemask, dt)
  507.                 if self.mode==0 then ApplyHeightBrush(terrain,hmap,mask,gx,gz,self.radius, self.max, self.power, self.hardness, self.usemask, dt) terrain:ApplyHeightMap()
  508.                 elseif self.mode>=1 and self.mode<=8 then ApplyBlendBrush8(terrain,hmap,blend1,blend2,mask,gx,gz,self.radius,self.max,self.power,self.hardness,self.mode-1,self.usemask,dt) blendtex1:SetData(blend1) blendtex2:SetData(blend2)
  509.                 elseif self.mode==9 then ApplySmoothBrush(terrain,hmap,mask,gx,gz,self.radius, self.max, self.power, self.hardness, self.usemask, dt) terrain:ApplyHeightMap()
  510.                 else ApplyMaskBrush(terrain,hmap,mask,gx,gz,self.radius,self.max,self.power,self.hardness,dt) masktex:SetData(mask)
  511.                 end
  512.             end
  513.            
  514.         end
  515.     elseif input:GetKeyPress(KEY_W) then
  516.         local mouseground=cam:PickGround(mousepos.x, mousepos.y)
  517.         self:AddWaypoint(mouseground.x, mouseground.z)
  518.        
  519.     elseif input:GetKeyPress(KEY_Q) then
  520.         if(#waypoints>0) then
  521.             waypoints[#waypoints]:Remove()
  522.             table.remove(waypoints)
  523.         end
  524.         self:UpdateWaypointVis()
  525.     end
  526.    
  527.     local c
  528.     for _,c in ipairs(waypoints) do
  529.         local ht=terrain:GetHeight(Vector3(c.position.x,0,c.position.z))
  530.         c.position=Vector3(c.position.x,ht,c.position.z)
  531.     end
  532.     self:UpdateWaypointVis()
  533. end
  534.  
  535. function TerrainEditUI:GenerateBrushPreview(sharpness)
  536.     local w,h=self.brushpreview:GetWidth(), self.brushpreview:GetHeight()
  537.     local rad=w/2
  538.     local x,y
  539.     for x=0,w-1,1 do
  540.         for y=0,h-1,1 do
  541.             local dx=x-w/2
  542.             local dy=y-h/2
  543.             local d=math.sqrt(dx*dx+dy*dy)
  544.             --local i=(rad-d)/rad
  545.             local i=(d-rad)/(sharpness*rad-rad)
  546.             i=math.max(0, math.min(1,i))
  547.                
  548.             self.brushpreview:SetPixel(x,y,Color(i*0.5,i*0.5,i*0.6))
  549.         end
  550.     end
  551.    
  552.     self.brushtex:SetData(self.brushpreview, false)
  553. end
  554.  
  555. function TerrainEditUI:HandleButtonPress(eventType, eventData)
  556.     local which=eventData["Element"]:GetPtr("UIElement")
  557.     local name=which:GetName()
  558.     if name=="HeightButton" then
  559.         self.mode=0    
  560.         self:ActivateHeightBrush()
  561.        
  562.     elseif name=="SmoothButton" then
  563.         self.mode=9
  564.         self:ActivateSmoothBrush()
  565.    
  566.     elseif name=="Terrain1Button" then
  567.         self.mode=1
  568.         self:ActivateBlendBrush()
  569.    
  570.     elseif name=="Terrain2Button" then
  571.         self.mode=2
  572.         self:ActivateBlendBrush()
  573.    
  574.     elseif name=="Terrain3Button" then
  575.         self.mode=3
  576.         self:ActivateBlendBrush()
  577.    
  578.     elseif name=="Terrain4Button" then
  579.         self.mode=4
  580.         self:ActivateBlendBrush()
  581.     elseif name=="Terrain5Button" then
  582.         self.mode=5
  583.         self:ActivateBlendBrush()
  584.     elseif name=="Terrain6Button" then
  585.         self.mode=6
  586.         self:ActivateBlendBrush()
  587.     elseif name=="Terrain7Button" then
  588.         self.mode=7
  589.         self:ActivateBlendBrush()
  590.     elseif name=="Terrain8Button" then
  591.         self.mode=8
  592.         self:ActivateBlendBrush()
  593.    
  594.    
  595.     elseif name=="MaskButton" then
  596.         self.mode=10
  597.         self:ActivateMaskBrush()
  598.    
  599.     elseif name=="FilterButton" then
  600.         if self.filterui.visible==true then self.filterui.visible=false
  601.         else
  602.             print("Showing filters")
  603.             self:PopulateFilterList()
  604.             self.filterui.visible=true
  605.         end
  606.     elseif name=="ExecuteButton" then
  607.         if self.selectedfilter then
  608.             -- Grab options
  609.             if self.selectedfilter.options ~= nil then
  610.                 local c
  611.                 for _,c in ipairs(self.selectedfilter.options) do
  612.                     local element=self.filteroptions:GetChild(c.name, true)
  613.                     if element then
  614.                         if c.type=="value" then c.value=tonumber(element.textElement.text)
  615.                         elseif c.type=="flag" then c.value=element.checked
  616.                         end
  617.                     end
  618.                 end
  619.             end
  620.             self.selectedfilter:execute()
  621.             collectgarbage()
  622.             print("Usedmem: "..collectgarbage("count"))
  623.         end
  624.     elseif name=="RescanFilters" then
  625.         self:PopulateFilterList()
  626.     elseif name=="ClearMask" then
  627.         mask:Clear(Color(1,1,1))
  628.         masktex:SetData(mask)
  629.     end
  630.    
  631. end
  632.  
  633. function TerrainEditUI:HandleSliderChanged(eventType, eventData)
  634.     local which=eventData["UIElement"]:GetPtr("Element")
  635.    
  636.     if which==nil then return end
  637.    
  638.     self.power, self.max, self.radius, self.hardness, self.usemask=self:GetBrushSettings(self.activebrush)
  639.     self:BuildCursorMesh(self.radius)
  640.     self:GenerateBrushPreview(self.hardness)
  641.    
  642.     if which==self.activebrush:GetChild("PowerSlider", true) then
  643.         local text=self.activebrush:GetChild("PowerText", true)
  644.         if text then text.text=string.format("%.2f", self.power) end
  645.     elseif which==self.activebrush:GetChild("RadiusSlider", true) then
  646.         local text=self.activebrush:GetChild("RadiusText", true)
  647.         if text then text.text=tostring(math.floor(self.radius)) end
  648.     elseif which==self.activebrush:GetChild("MaxSlider", true) then
  649.         local text=self.activebrush:GetChild("MaxText", true)
  650.         if text then text.text=string.format("%.2f", self.max) end
  651.     elseif which==self.activebrush:GetChild("HardnessSlider", true) then
  652.         local text=self.activebrush:GetChild("HardnessText", true)
  653.         if text then text.text=string.format("%.3f", self.hardness) end
  654.     end
  655. end
  656.  
  657. function TerrainEditUI:HandleToggled(eventType, eventData)
  658.     local which=eventData["UIElement"]:GetPtr("Element")
  659.     if which==nil then return end
  660.        
  661. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top