StrataCafe-Forums/その4 教本2

Last-modified: 2008-09-15 (月) 18:03:08

Note: The format of these tutorials may change over time. We're just getting started here in this forum, so there may be a bit of a growing pain as we find the best format and way to put these tutorials up.
注:これらの教本のフォーマットは時間とともに変わるかもしれません。私たちは、このフォーラムでここでちょうど始めようとしています。したがって、私たちが最良のフォーマットおよびこれらの教本を上げる方法を見つけるとともに、ちょっとした成長している苦痛があるかもしれません。

Author: jbradley
著者:jbradley

Overview:
概観:
In Part I we created a function that would allow us to replicate a selected object in our scene a certain number of times.
一部分私たちが作ったI、私たちが私たちの場面の選択されたオブジェクトを模写することを可能にする機能、幾らかの回。

In this example we'll create a UI dialog that will allow you to choose the number of copies that will be passed to the Replicate function we've created.
この例において、私たちは、あなたが、私たちが作成した折り返した機能に渡されるコピーの数を選ぶことを可能にするUI対話を作成するでしょう。

The final script for Part II
部分IIのための最終スクリプト
Copy / Paste the following into your Console (Scripting -> Console menu). Select the text and press "Evaluate" on the lower left of the console window.
コピー/ペースト、あなたのコンソール(スクリプトを書く>コンソール・メニュー)の中への下記。テキストを選択して、「評価する」ことをコンソール・ウィンドウのより低い左側に押してください。

-- A function to replicate a selected object a certain number of times
function Replicate(numCopies)
	local c = s3d.database.GetActiveContext()
	local m = c and c:GetManipulatorManager()
	local size = m and m:GetSize()
	if size > 0 then
       local i = m:GetIndexedInstance(1)
        for t=1, numCopies do
           print("Creating copy ".. t)
           local inst = i:Clone()
           c:AddInstance(inst)
           --	Random translation within 5 inch boundary
           local tx = math.random(360) - math.random(360)
           local ty = math.random(360) - math.random(360)
           local tz = math.random(360) - math.random(360)
           local position = s3d.vec.Point3d(tx,ty,tz)
           --	Make sure our objects are at least 0.5 inches plus some random deviation
           local ns = 36 + math.random(72)
           local scale = s3d.vec.Point3d(ns,ns,ns)
           local vector = s3d.vec.Dir3d(math.random(), math.random(), math.random()):Normalize()
	    local angle = math.random()*2*math.pi
           local w = math.cos(angle/2)
           local qx = vector.x * math.sin(angle/2)
           local qy = vector.y * math.sin(angle/2)
           local qz = vector.z * math.sin(angle/2)
           local rotation = s3d.vec.Quaternion(qx,qy,qz,w)
           inst:SetTranslationAt(0,position)
           inst:SetScaleAt(0,scale)
           inst:SetRotationAt(0, rotation )
       end
       c:Refresh()
	else
		s3d.ui.Alert ("You must select an object.")
	end
end
function RandomReplicateUI()
	local d = s3d.ui.Dialog("Random Replicate")
	d:AddStaticText("Number of Copies")
	local numCopies = d:AddNumberText(0,0,40)
	d:SetLabel(s3d.ui.Dialog.kOkay, "Replicate")
	d:SetLabel(s3d.ui.Dialog.kCancel, "Cancel")
	local function checkUI(id)
		if id == s3d.ui.Dialog.kOkay then
			local num = d:GetValue(numCopies)
			if num ~= 0 then
				Replicate(num)
			end
		end
	end
	d:Present( checkUI )
end

Creating the Dialog:
対話の作成:
If you haven't already read through Mark's tutorial on UI Dialogs, now would be a good time to do so. We'll build upon that knowledge here and avoid explaining things that are covered in previous tutorials unless there's additional information we want to point out.
今、もしあなたがまだマークのUI対話中の教本を通読していなければ、そうするのによい時間があるでしょう。私たちはここのその知識に基礎を置き、もし私たちが指摘したい追加情報がなければ、前の教本でカバーされるものについて説明しないようにするでしょう。

When building a UI dialog, it's good practice to enclose the dialog in a function that will be called when needed. This is a safe approach so that you do not 'pollute' the global table of scripting functionality in CX 5 with unnecessary variables and functions.
UI対話を構築する場合、それは、必要だった時呼ばれる関数で対話を囲むのによい実行です。あなたが不必要な変数および機能を備えたCX 5の中の機能性をスクリプトにするグローバルなテーブルを「汚染しない」ように、これは安全なアプローチです。

Let's first determine what our goals are for this dialog:
この対話用の私たちのゴールの状態を最初に決定しましょう:
Need an input box - integer values only - for the number of copies we want to make of a selected object.
私たちが選択されたオブジェクトで作りたいコピーの数に入力箱(整数価値だけ)を必要としてください。
Need a cancel and OK button to initiate the replication and to make sure the user entered a valid number
応答を始めて、かつユーザが有効な数を入力したことを確かめるためにcancelおよびよろしいボタンを必要とします。

Ok, let's begin. We will name our Replication UI dialog function RandomReplicateUI. This ensures that there's a noticeable connection between the two functions. One does the replication, one is for the UI. This is a convention used in other 3d applications to separate the functional code from the user-interface code.
Ok、始まりましょう。私たちは応答UI対話機能をRandomReplicateUIと命名するでしょう。これは、顕著な接続が2つの機能間にあることを保証します。1つは応答を行います、一つはUI向けです。これは、ユーザ・インターフェース・コードから機能的なコードを分けるために他の3d適用の中で使用される協定です。

function RandomReplicateUI()
end

Above is the base format for our UI function. Now, knowing what we know from Mark's tutorial, we'll add in our UI code to build a dialog with a title, an input field (and description of the field), and Cancel and OK dialog buttons.
上記のことは私たちのUI機能のための基礎フォーマットです。さて、私たちがマークの教本と何を区別ができるか知って、私たちはするでしょう、アドイン、タイトル(入力フィールド(またフィールドの記述))との対話を構築し、かつ取り消す私たちのUIコードおよびよろしい対話ボタン。

function RandomReplicateUI()
	d = s3d.ui.Dialog("Random Replicate")
	d:AddStaticText("Number of Copies:")
	d:AddColumn()
	numCopies = d:AddNumberText(1)
	d:SetLabel(s3d.ui.Dialog.kOkay, "OK")
	d:SetLabel(s3d.ui.Dialog.kCancel, "Cancel")
	d:Present()
end

Ok. So, if we now type and evaluate RandomReplicateUI() in our console, we'll get a dialog that pops up with what we expect. Let's make a few changes to this code...
Ok。したがって、今コンソール中のRandomReplicateUI()をタイプし評価すれば、私たちは私たちが期待するもので突然飛び出る対話を得るでしょう。このコードに少数の変更を加えましょう...

function RandomReplicateUI()
	local d = s3d.ui.Dialog("Random Replicate")
	d:AddStaticText("Number of Copies")
	d:AddColumn()
	local numCopies = d:AddNumberText(0,0,40)
	d:SetLabel(s3d.ui.Dialog.kOkay, "Replicate")
	d:SetLabel(s3d.ui.Dialog.kCancel, "Cancel")
	d:Present()
end

What changes did we make?
私たちはどの変更を加えましたか。

local d = s3d.ui.Dialog("Random Replicate")

ローカルのd=s3d.ui.Dialog 「 ランダム(折れ重なる。」)

We want to ensure that variables exist only inside a function are local, unless we're using variables that are outside the scope of the function's internals. For more information on the Lua convention for variables and functions, please check lua.org or lua-users.org for tutorials on the Lua syntax.
私たちは、変数が機能の内部でのみ存在することを保証したい、もし私たちが機能の内部の範囲の外にある変数を使用していなければ、局所的です。変数と機能のためのLua協定についてより詳細には、Luaシンタックス上の教本があるかlua.orgあるいはlua-users.orgをチェックしてください。

local numCopies = d:AddNumberText(0,0,40)

ローカルのnumCopies=d:AddNumberText(0、0、40)

Same as above. Keep numCopies a local variable. A few things to note here. numCopies is not the value entered in the input box. It is a control reference to what the dialog is storing. We'll show you how to access the actual value a bit later on in this tutorial. We also added a few parameters to the function. AddNumberText takes up to three arguments: AddNumberText(InitialValue,DecimalPlaces,PixelWidthOfInputBox)
同じ、として、の上に。numCopiesをローカル変数にしておいてください。ここで注意する少数のもの。numCopiesは入力箱に入力された値ではありません。それは対話が格納しているものへのコントロール言及です。私たちは、この教本の中で実価に少し後でアクセスする方法をあなたに教えるでしょう。さらに、私たちは機能に少数のパラメーターを加えました。AddNumberTextは3つまでの議論をとります:AddNumberText(デフォルト値、小数位、PixelWidthOfInputBox)

Alright. We've created our UI dialog, but there's something major missing. How do we grab the value that the user entered and properly pass it off to our Replicate() function?
申し分ありません。私たちはUI対話を作成しました、しかし、主なものがあります、見当たらない私たちは、どのようにユーザが入力し、適切に私たちの折り返した()機能にそれに離れて渡す値をつかみますか。

Well, the Present method of the Dialog class in CX 5 actually takes a function as an argument. The function passed to the Present() method is called when any control (button, checkbox, radio, etc.) is pressed in the dialog. It allows you to make decisions on any user action in the dialog when a button is pressed. In our case, we want to actually grab the input value (if the OK button was pressed), and pass it to our Replicate function.
さて、CX 5の中の対話クラスの現在の方法は、実際に議論として機能をとります。どんなコントロール(ボタン、チェックボックス、ラジオなど)も対話で押される場合、現在()方法に渡された関数が呼ばれます。それは、ボタンが押される場合、あなたが対話で任意のユーザ・アクションに決定することを可能にします。私たちのケースでは、私たちは入力値(よろしいボタンが押された場合)を実際につかみ、それを折り返した機能へ渡したい。

This is how it looks:
このようにして、それは見ます:

function RandomReplicateUI()
	local d = s3d.ui.Dialog("Random Replicate")
	d:AddStaticText("Number of Copies")
	local numCopies = d:AddNumberText(0,0,40)
	d:SetLabel(s3d.ui.Dialog.kOkay, "Replicate")
	d:SetLabel(s3d.ui.Dialog.kCancel, "Cancel")
	local function checkUI(id)
	end
	d:Present( checkUI )
end

When any dialog control is pressed, the function checkUI will be called and passed an ID of the control that was pressed by the instance of our Dialog class (d). Now that we have this set up, we need to ensure that the OK button was pressed and that we have a value greater than 0 that the user entered into the input box.
どんな対話コントロールも押される時、関数checkUIは呼ばれ私たちの対話クラス(d)の実例によって押されたコントロールのIDを渡されるでしょう。今これをセット・アップするので、私たちは、よろしいボタンが押され、私たちが、ユーザが入力箱へ入力した、0を越える値を持っていることを保証する必要があります。

Let's make those final changes:
それらの最終変更を加えましょう:

function RandomReplicateUI()
	local d = s3d.ui.Dialog("Random Replicate")
	d:AddStaticText("Number of Copies")
	local numCopies = d:AddNumberText(0,0,40)
	d:SetLabel(s3d.ui.Dialog.kOkay, "Replicate")
	d:SetLabel(s3d.ui.Dialog.kCancel, "Cancel")
	local function checkUI(id)
		if id == s3d.ui.Dialog.kOkay then
			local num = d:GetValue(numCopies)
		end
	end
	d:Present( checkUI )
end

Now, place the code above in your Console and press evaluate. Notice that really nothing has changed in our visual result. This is because we're making small changes behind the scenes that are necessary for proper handling of the input.
さて、あなたのコンソールと圧迫にコードを上に置く、評価します。実際に、何も私たちの視覚的な結果の中で変わっていないことに注目してください。これは、私たちが入力の適切な取り扱いに必要な小銭を陰で作っているからです。

Let's use the print statement of Lua to see what our value the user entered is, before we get into actually running our Replicate function.
ユーザが入力した私たちの値は私たちが実際に私たちの折り返した機能を実行することに入る前に、何か確かめるためにLuaの印刷ステートメントを使用しましょう。

function RandomReplicateUI()
	local d = s3d.ui.Dialog("Random Replicate")
	d:AddStaticText("Number of Copies")
	local numCopies = d:AddNumberText(0,0,40)
	d:SetLabel(s3d.ui.Dialog.kOkay, "Replicate")
	d:SetLabel(s3d.ui.Dialog.kCancel, "Cancel")
	local function checkUI(id)
		if id == s3d.ui.Dialog.kOkay then
			local num = d:GetValue(numCopies)
			print("User entered: "..num)
		end
	end
	d:Present( checkUI )
end

Now, if you evaluate the code above, you'll see the results of the print statement in the Console, directly under the text selection that you evaluated. It probably would be nice to note this earlier, but the "Evaluate" button in the Console will execute the selected text anywhere in the console. If there are errors or results, they will be output to the console directly under the selected text in non-bold text, while the executed code will be bold.
さて、上記のコードを評価すれば、評価したテキスト選択の下でコンソールに印刷ステートメントの結果を直接見出すでしょう。これに以前に注意することは恐らくよいでしょう。しかし、コンソール中の「評価する」ボタンは?I択されたテキストをコンソールのいかなる場所にも実行するでしょう。エラーまたは結果があれば、実行されたコードが大胆になっている一方、それらは非大胆なテキストの中で選択されたテキストの下でコンソールに直接出力されるでしょう。

As our last step, we'll add in our function call Replicate() and pass it the value "num" to actually perform our replication.
私たちの最後のステップとして、私たちはするでしょう、アドイン、私たちの関数呼び出し()を模写し、それを通る、実際に私たちの応答を行なう値「num。」

Final Code for Part II:
部分IIのための最終コード:

-- A function to replicate a selected object a certain number of times
function Replicate(numCopies)
	local c = s3d.database.GetActiveContext()
	local m = c and c:GetManipulatorManager()
	local size = m and m:GetSize()
	if size > 0 then
       local i = m:GetIndexedInstance(1)
        for t=1, numCopies do
           print("Creating copy ".. t)
           local inst = i:Clone()
           c:AddInstance(inst)
           --	Random translation within 5 inch boundary
           local tx = math.random(360) - math.random(360)
           local ty = math.random(360) - math.random(360)
           local tz = math.random(360) - math.random(360)
           local position = s3d.vec.Point3d(tx,ty,tz)
           --	Make sure our objects are at least 0.5 inches plus some random deviation
           local ns = 36 + math.random(72)
           local scale = s3d.vec.Point3d(ns,ns,ns)
           local vector = s3d.vec.Dir3d(math.random(), math.random(), math.random()):Normalize()
	    local angle = math.random()*2*math.pi
           local w = math.cos(angle/2)
           local qx = vector.x * math.sin(angle/2)
           local qy = vector.y * math.sin(angle/2)
           local qz = vector.z * math.sin(angle/2)
           local rotation = s3d.vec.Quaternion(qx,qy,qz,w)
           inst:SetTranslationAt(0,position)
           inst:SetScaleAt(0,scale)
           inst:SetRotationAt(0, rotation )
       end
       c:Refresh()
	else
		s3d.ui.Alert ("You must select an object.")
	end
end
function RandomReplicateUI()
	local d = s3d.ui.Dialog("Random Replicate")
	d:AddStaticText("Number of Copies")
	local numCopies = d:AddNumberText(0,0,40)
	d:SetLabel(s3d.ui.Dialog.kOkay, "Replicate")
	d:SetLabel(s3d.ui.Dialog.kCancel, "Cancel")
	local function checkUI(id)
		if id == s3d.ui.Dialog.kOkay then
			local num = d:GetValue(numCopies)
			if num ~= 0 then
				Replicate(num)
			end
		end
	end
	d:Present( checkUI )
end

Again, copy/paste all the code directly above in the console, press "Evaluate" and finally, type in RandomReplicateUI(), select it and press Evaluate in the Console to set it in motion.
再び、コピー/ペースト、すべてのコード、コンソールの中で直接上に、「評価する」ことを押し、最後に、RandomReplicateUI()をタイプインする、それと圧迫を選択する、それを推進するためにコンソールの中で評価します。

Neat, huh?
きれい、へえー?

In Part III of this tutorial, we'll learn how to add this UI dialog function to our Scripting Menu in the application and store the code in an external text file so that it's loaded when CX 5 starts up.
一部分この教本のIII、私たちは、私たちが適用でのメニューをスクリプトにすることにこのUI対話機能を加えて、格納する方法を学習するでしょう、CX 5が開始する場合、それがロードされるように、外部テキストファイルの中でコード化します。

Jon Bradley